如何多次上传单张图片(超过100次)?

时间:2012-10-15 10:33:02

标签: image image-processing file-upload for-loop asp.net-mvc-4

大家好我正在研究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);

                ImageModel.ResizeAndSave(thumsise, fileName, model.ImageUploaded.InputStream, 80, true);

            }
            return View("Upload",model);
        }

这是我的索引页面:

@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 />
}

你能帮我做一下吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

如何编写for循环:

[HttpPost]
public ActionResult Uploading(ImageModel model)
{
    if (ModelState.IsValid)
    {     
        string imagesPath = Server.MapPath("~/Content/Images");
        string fileName = Guid.NewGuid().ToString();
        for (var i = 1; i <= 100; i++) 
        {
            string thumsise = Path.Combine(
                imagesPath, 
                string.Format("Thumb{0}_{1}", fileName, i)
            );
            ImageModel.ResizeAndSave(thumsise, fileName, model.ImageUploaded.InputStream, 80, true);
        }
        return View("Upload",model);
    }
}

这将在~/Content/Images文件夹中创建100个图像,方法是在Guid前加上Thumb,并在后面添加循环索引。