大家好,我在 mvc4 上工作,我已经上传了单个图像(文件),但现在我需要一次将 100 多个文件(图像)加载到我的应用程序中,我该怎么做?请帮忙
这是我的代码:这是我的控制器
[HttpPost]
public ActionResult Uploading(ImageModel model)
{
if (ModelState.IsValid)
{
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, model.ImageUploaded.InputStream, 80, true);
ImageModel.ResizeAndSave(thumbPath, fileName, model.ImageUploaded.InputStream, 100, true);
ImageModel.ResizeAndSave(fullPath, fileName, model.ImageUploaded.InputStream, 500, true);
ImageModel.ResizeAndSave(Bigpath, fileName, model.ImageUploaded.InputStream, 200, true);
ImageModel.ResizeAndSave(Bigpatha, fileName, model.ImageUploaded.InputStream, 250, true);
ImageModel.ResizeAndSave(Bigpathb, fileName, model.ImageUploaded.InputStream, 150, true);
ImageModel.ResizeAndSave(Bigpathc, fileName, model.ImageUploaded.InputStream, 50, true);
}
return View("Upload",model);
}
这是我的类文件:
public class ImageModel
{
[Required]
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();
}
}
}
这是我的索引页:
@using (Html.BeginForm("Uploading", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*" />
<button type="submit" id="Upload">Upload</button>
<br />
}
你能帮我一次上传100多个文件吗提前谢谢