【问题标题】:How to upload single image multipletimes(morethan 100 times)?如何多次上传单张图片(超过100次)?
【发布时间】:2012-10-05 07:37:00
【问题描述】:

大家好,我正在研究 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 />
}

你能帮我做这件事吗?提前致谢。

【问题讨论】:

  • 为什么要将同一张图片保存 100 次?
  • 其实需要找到loop的时间耗时
  • 1 次或 100 次循环的时间?仍然不明白为什么你需要一个 100
  • 谢谢mr.dove你花时间帮我完成任务

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


【解决方案1】:

写一个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 并在其后面加上循环索引。

【讨论】:

  • 感谢 Darin Dimitrov 先生,它为我工作了很好的编码,非常感谢您,感谢您对我的回复
猜你喜欢
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多