こんにちは、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 個以上のファイルをアップロードするのを手伝ってもらえますか?