Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC4上传图像并插入数据库_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_File Upload - Fatal编程技术网

C# MVC4上传图像并插入数据库

C# MVC4上传图像并插入数据库,c#,asp.net,asp.net-mvc,asp.net-mvc-4,file-upload,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,File Upload,几天前我在MVC4 visual studio工作。我真的是新手,希望任何人都能帮助我 我在我的网站上有一个表单,输入类型为text,输入类型为file 我想在数据库中插入一个包含多个imageuploads的新项目 我尝试在视图、控制器和模型中这样做 我搜索了一些东西,并找到了一个代码谁帮助了很多上传的几个图像 但我想要的是,当我点击按钮时,上传所有图像,并将其他字段的所有信息和所有图像的文件名插入到我的表中。因此,我可以在另一个页面中选择与此id关联的所有图像 希望有人能帮上忙 这是我的密码

几天前我在MVC4 visual studio工作。我真的是新手,希望任何人都能帮助我

我在我的网站上有一个表单,输入类型为text,输入类型为file

我想在数据库中插入一个包含多个imageuploads的新项目

我尝试在视图、控制器和模型中这样做

我搜索了一些东西,并找到了一个代码谁帮助了很多上传的几个图像

但我想要的是,当我点击按钮时,上传所有图像,并将其他字段的所有信息和所有图像的文件名插入到我的表中。因此,我可以在另一个页面中选择与此id关联的所有图像

希望有人能帮上忙

这是我的密码:

提前谢谢

查看:

@using (Html.BeginForm("Uploading", "AdicionarRevista", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<label for="file">Nome da Empresa:</label>   
<input type="text" name="Name" id="nome" /><br />

<label for="file">Titulo da Revista:</label>       
<input type="text" name="Title" id="titulo" /> <br />

<label for="file">Upload:</label>         
<input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*"  />    <br />

<button type="submit"  id="Upload">Upload</button>
}
  [HttpPost]
        public ActionResult Uploading(ImageModel infouploadimage)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    HttpPostedFileBase uploadedFile = Request.Files[i];
                    string fileName = Guid.NewGuid().ToString();
                    string serverPath = Server.MapPath("~");
                    string imagesPath = serverPath + "Content\\Images\\";
                    string thumsise = Path.Combine(imagesPath, "Thumb" + fileName);
                    string thumbPath = Path.Combine(imagesPath, "Thu" + fileName);
                    string fullPath = Path.Combine(imagesPath, "Full" + fileName);
                    string Bigpath = Path.Combine(imagesPath, "big" + fileName);
                    string Bigpatha = Path.Combine(imagesPath, "biga" + fileName);
                    string Bigpathb = Path.Combine(imagesPath, "bigb" + fileName);
                    string Bigpathc = Path.Combine(imagesPath, "bigc" + fileName);
                    ImageModel.ResizeAndSave(thumsise, fileName, uploadedFile.InputStream, 80, true);
                    ImageModel.ResizeAndSave(thumbPath, fileName, uploadedFile.InputStream, 100, true);
                    ImageModel.ResizeAndSave(fullPath, fileName,  uploadedFile.InputStream, 500, true);
                    ImageModel.ResizeAndSave(Bigpath, fileName, uploadedFile.InputStream, 200, true);
                    ImageModel.ResizeAndSave(Bigpatha, fileName, uploadedFile.InputStream, 250, true);
                    ImageModel.ResizeAndSave(Bigpathb, fileName, uploadedFile.InputStream, 150, true);
                    ImageModel.ResizeAndSave(Bigpathc, fileName, uploadedFile.InputStream, 50, true);

                }
            }


            return RedirectToAction("Index", "Home");
        }
 public class ImageModel
    {

        [Required]

        public string Name{ get; set; }
        public string Title{ get; set; }

        public HttpPostedFileWrapper ImageUploaded { get; set; }
        public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            //newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            image.Dispose();
            newImage.Dispose();
        }

        public void infouploadimage(string name, string title,string filename)
        {
            Name= name;
            Title= title;
           ImageUploaded = filename; <== ERROR HERE ALSO

            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection connect = new SqlConnection(connection))
            {
                string query = "Insert Into MYtable(Name, Title, Images) Values(@name, @title, ???)";

                SqlCommand command = new SqlCommand(query, connect);
                command.Parameters.AddWithValue("NomeEmpresa", n_empresa);
                command.Parameters.AddWithValue("Titulo", titulo_revista);
                //command.Parameters.AddWithValue("filename", ImageUploaded);
                connect.Open();
                command.ExecuteNonQuery();
            }
        }
}
@使用(Html.BeginForm(“上传”,“AdicionarRevista”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
Nome da Empresa:

提图洛·达雷维斯塔:
上传:
上传 }
控制器:

@using (Html.BeginForm("Uploading", "AdicionarRevista", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<label for="file">Nome da Empresa:</label>   
<input type="text" name="Name" id="nome" /><br />

<label for="file">Titulo da Revista:</label>       
<input type="text" name="Title" id="titulo" /> <br />

<label for="file">Upload:</label>         
<input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*"  />    <br />

<button type="submit"  id="Upload">Upload</button>
}
  [HttpPost]
        public ActionResult Uploading(ImageModel infouploadimage)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    HttpPostedFileBase uploadedFile = Request.Files[i];
                    string fileName = Guid.NewGuid().ToString();
                    string serverPath = Server.MapPath("~");
                    string imagesPath = serverPath + "Content\\Images\\";
                    string thumsise = Path.Combine(imagesPath, "Thumb" + fileName);
                    string thumbPath = Path.Combine(imagesPath, "Thu" + fileName);
                    string fullPath = Path.Combine(imagesPath, "Full" + fileName);
                    string Bigpath = Path.Combine(imagesPath, "big" + fileName);
                    string Bigpatha = Path.Combine(imagesPath, "biga" + fileName);
                    string Bigpathb = Path.Combine(imagesPath, "bigb" + fileName);
                    string Bigpathc = Path.Combine(imagesPath, "bigc" + fileName);
                    ImageModel.ResizeAndSave(thumsise, fileName, uploadedFile.InputStream, 80, true);
                    ImageModel.ResizeAndSave(thumbPath, fileName, uploadedFile.InputStream, 100, true);
                    ImageModel.ResizeAndSave(fullPath, fileName,  uploadedFile.InputStream, 500, true);
                    ImageModel.ResizeAndSave(Bigpath, fileName, uploadedFile.InputStream, 200, true);
                    ImageModel.ResizeAndSave(Bigpatha, fileName, uploadedFile.InputStream, 250, true);
                    ImageModel.ResizeAndSave(Bigpathb, fileName, uploadedFile.InputStream, 150, true);
                    ImageModel.ResizeAndSave(Bigpathc, fileName, uploadedFile.InputStream, 50, true);

                }
            }


            return RedirectToAction("Index", "Home");
        }
 public class ImageModel
    {

        [Required]

        public string Name{ get; set; }
        public string Title{ get; set; }

        public HttpPostedFileWrapper ImageUploaded { get; set; }
        public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            //newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            image.Dispose();
            newImage.Dispose();
        }

        public void infouploadimage(string name, string title,string filename)
        {
            Name= name;
            Title= title;
           ImageUploaded = filename; <== ERROR HERE ALSO

            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection connect = new SqlConnection(connection))
            {
                string query = "Insert Into MYtable(Name, Title, Images) Values(@name, @title, ???)";

                SqlCommand command = new SqlCommand(query, connect);
                command.Parameters.AddWithValue("NomeEmpresa", n_empresa);
                command.Parameters.AddWithValue("Titulo", titulo_revista);
                //command.Parameters.AddWithValue("filename", ImageUploaded);
                connect.Open();
                command.ExecuteNonQuery();
            }
        }
}
[HttpPost]
公共操作结果上载(ImageModel infouploadimage)
{
对于(int i=0;i0)
{
HttpPostedFileBase uploadedFile=Request.Files[i];
字符串文件名=Guid.NewGuid().ToString();
字符串serverPath=Server.MapPath(“~”);
字符串imagesPath=serverPath+“Content\\Images\\”;
字符串thumsse=Path.Combine(imagesPath,“Thumb”+文件名);
字符串thumbPath=Path.Combine(imagesPath,“Thu”+文件名);
字符串fullPath=Path.Combine(imagesPath,“Full”+文件名);
字符串Bigpath=Path.Combine(imagesPath,“big”+文件名);
字符串Bigpatha=Path.Combine(imagesPath,“biga”+文件名);
字符串Bigpathb=Path.Combine(imagesPath,“bigb”+文件名);
字符串Bigpathc=Path.Combine(imagesPath,“bigc”+文件名);
ResizeAndSave(thumsse,文件名,uploadedFile.InputStream,80,true);
ResizeAndSave(thumbPath,文件名,uploadedFile.InputStream,100,true);
ResizeAndSave(完整路径,文件名,uploadedFile.InputStream,500,true);
ResizeAndSave(Bigpath,文件名,uploadedFile.InputStream,200,true);
ResizeAndSave(Bigpatha,文件名,uploadedFile.InputStream,250,true);
ResizeAndSave(Bigpathb,文件名,uploadedFile.InputStream,150,true);
ResizeAndSave(Bigpathc,文件名,uploadedFile.InputStream,50,true);
}
}
返回重定向到操作(“索引”、“主页”);
}
型号:

@using (Html.BeginForm("Uploading", "AdicionarRevista", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<label for="file">Nome da Empresa:</label>   
<input type="text" name="Name" id="nome" /><br />

<label for="file">Titulo da Revista:</label>       
<input type="text" name="Title" id="titulo" /> <br />

<label for="file">Upload:</label>         
<input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*"  />    <br />

<button type="submit"  id="Upload">Upload</button>
}
  [HttpPost]
        public ActionResult Uploading(ImageModel infouploadimage)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    HttpPostedFileBase uploadedFile = Request.Files[i];
                    string fileName = Guid.NewGuid().ToString();
                    string serverPath = Server.MapPath("~");
                    string imagesPath = serverPath + "Content\\Images\\";
                    string thumsise = Path.Combine(imagesPath, "Thumb" + fileName);
                    string thumbPath = Path.Combine(imagesPath, "Thu" + fileName);
                    string fullPath = Path.Combine(imagesPath, "Full" + fileName);
                    string Bigpath = Path.Combine(imagesPath, "big" + fileName);
                    string Bigpatha = Path.Combine(imagesPath, "biga" + fileName);
                    string Bigpathb = Path.Combine(imagesPath, "bigb" + fileName);
                    string Bigpathc = Path.Combine(imagesPath, "bigc" + fileName);
                    ImageModel.ResizeAndSave(thumsise, fileName, uploadedFile.InputStream, 80, true);
                    ImageModel.ResizeAndSave(thumbPath, fileName, uploadedFile.InputStream, 100, true);
                    ImageModel.ResizeAndSave(fullPath, fileName,  uploadedFile.InputStream, 500, true);
                    ImageModel.ResizeAndSave(Bigpath, fileName, uploadedFile.InputStream, 200, true);
                    ImageModel.ResizeAndSave(Bigpatha, fileName, uploadedFile.InputStream, 250, true);
                    ImageModel.ResizeAndSave(Bigpathb, fileName, uploadedFile.InputStream, 150, true);
                    ImageModel.ResizeAndSave(Bigpathc, fileName, uploadedFile.InputStream, 50, true);

                }
            }


            return RedirectToAction("Index", "Home");
        }
 public class ImageModel
    {

        [Required]

        public string Name{ get; set; }
        public string Title{ get; set; }

        public HttpPostedFileWrapper ImageUploaded { get; set; }
        public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            //newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            image.Dispose();
            newImage.Dispose();
        }

        public void infouploadimage(string name, string title,string filename)
        {
            Name= name;
            Title= title;
           ImageUploaded = filename; <== ERROR HERE ALSO

            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection connect = new SqlConnection(connection))
            {
                string query = "Insert Into MYtable(Name, Title, Images) Values(@name, @title, ???)";

                SqlCommand command = new SqlCommand(query, connect);
                command.Parameters.AddWithValue("NomeEmpresa", n_empresa);
                command.Parameters.AddWithValue("Titulo", titulo_revista);
                //command.Parameters.AddWithValue("filename", ImageUploaded);
                connect.Open();
                command.ExecuteNonQuery();
            }
        }
}
公共类ImageModel
{
[必需]
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共HttpPostedFileWrapper映像上载{get;set;}
公共静态void ResizeAndSave(字符串保存路径、字符串文件名、流图像缓冲区、int-maxSideSize、bool-makeItSquare)
{
int newWidth;
int newHeight;
Image=Image.FromStream(imageBuffer);
int oldWidth=image.Width;
int oldHeight=image.Height;
位图图像;
if(makeItSquare)
{
int smallerSide=oldWidth>=oldHeight?oldHeight:oldWidth;
双系数=maxSideSize/(双)smallerSide;
newWidth=Convert.ToInt32(系数*旧宽度);
新高度=转换为32(系数*旧高度);
位图tempImage=新位图(图像、新宽度、新高度);
intcropx=(newWidth-maxSideSize)/2;
int cropY=(newHeight-maxSideSize)/2;
newImage=新位图(maxSideSize,maxSideSize);
Graphics tempGraphic=Graphics.FromImage(新图像);
tempGraphic.SmoothingMode=SmoothingMode.AntiAlias;
tempGraphic.InterpolationMode=InterpolationMode.HighQualityBicubic;
tempGraphic.PixelOffsetMode=PixelOffsetMode.HighQuality;
tempGraphic.DrawImage(tempImage,新矩形(0,0,maxSideSize,MaxSideSide Size),cropX,cropY,MaxSideSide Size,MaxSideSide Size,GraphicsUnit.Pixel);
}
其他的
{
int maxSide=oldWidth>=oldHeight?oldWidth:oldHeight;
如果(maxSide>maxSideSize)
{
双系数=maxSideSize/(双)maxSide;
newWidth=Convert.ToInt32(系数*旧宽度);
新高度=转换为32(系数*旧高度);
}
其他的
{
新宽度=旧宽度;
新高度=旧高度;
}
newImage=新位图(图像、新宽度、新高度);
}
newImage.Save(保存路径+文件名+“.jpg”,ImageFormat.Jpeg);
//newImage.Save(保存路径+文件名+“.jpg”,ImageFormat.Jpeg);
image.Dispose();
Dispose();
}
public void infouploadimage(字符串名称、字符串标题、字符串文件名)
{
名称=名称;
头衔=头衔;

ImageUpload=filename;首先,在控制器中的POST方法内设置一个断点。调试时,让执行命中此断点,并验证模型是否按预期设置了所有属性。否则,将表单绑定到模型时可能会出现问题

另外,如果您熟悉SQL概要文件