【问题标题】:Multiple image upload mvc4多张图片上传mvc4
【发布时间】:2014-06-23 10:01:53
【问题描述】:

我在 IIS8.0 上使用 MVC4 将多个图像文件发布到服务器。这已经工作了很长时间,但最近该应用程序需要更改保存图像的位置。以前,我保存到网络位置,现在我直接保存到运行应用程序的同一网络服务器。

现在,当我上传时,只会保存一个文件。特别是列表中的最后一个文件。

为什么现在会发生这种情况?此外,我可以在 localhost 上运行该应用程序,它会将所有图像保存到 Web 服务器就可以了。一旦我发布并尝试它,它就会再次开始拍摄最终图像。

来自 HTML:

<form id="inputForm" method="post" action="../images/Create" enctype="multipart/form-data">

<div id="uploadList">
  <input type="file" name="files" id="file1" accept=".jpg,.png,.gif" />
  <input type="file" name="files" id="file2" accept=".jpg,.png,.gif" />
  <input type="file" name="files" id="file3" accept=".jpg,.png,.gif" />
  <input type="file" name="files" id="file4" accept=".jpg,.png,.gif" />
  <input type="file" name="files" id="file5" accept=".jpg,.png,.gif" />
  <input type="file" name="files" id="file6" accept=".jpg,.png,.gif" />
</div>

以下是 web.config 中的几行代码。

  <system.web>     
     <httpRuntime targetFramework="4.5" maxRequestLength="1048576" executionTimeout="3600" />
  </system.web>
<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="1073741824" />
  </requestFiltering>
</security>

来自控制器。

[HttpPost]
public ActionResult Create(IEnumerable<HttpPostedFileBase> files, string serial)

//... Other stuff

foreach (var file in files)
{
    if (file != null)
    {
        if (file.ContentType == "image/jpeg" || file.ContentType == "image/png" || file.ContentType == "image/gif")
        {
            var fileName = Path.GetExtension(file.FileName);
            string result = fileName.AppendTimeStamp();
            var path = Path.Combine((Server.MapPath("~/adpear\\") + storageLoc + "\\" + theUserCompany + "\\" + serial), (serial + "_" + result));
            file.SaveAs(path);
            theSavedImageCount++;
        }
    }
}

【问题讨论】:

  • 检查服务器上的路径变量,我认为所有文件都相同。所以你只能看到最后一个文件,因为它已经覆盖了所有其他文件。
  • 看起来你是对的...... appendtimestamp 函数正在执行 datetime.now.tostring("yyyyMMddHHmmssfff")。我会让它真正随机并再试一次。服务器会这么快是没有意义的。
  • 远程调试您的代码或至少将路径变量写入 txt 文件以进行调试。这就是我们如何了解实时值的方式。一旦完成,我们可以确定。
  • 我发布到服务器并将路径添加到 List 并将其作为 JSON 发送到浏览器。文件名如下。 “XXXX11111_20140506101324621.jpg”、“XXXX11111_20140506101324621.jpg”、“XXXX11111_20140506101324621.jpg”。完全一样。
  • 查看jQuery file upload 或者它的角度变体,它可以消除这样的痛苦:)

标签: c# asp.net-mvc asp.net-mvc-4 file-upload iis-8


【解决方案1】:

检查服务器上的path 变量,我认为所有文件都相同。如果你有相同的路径变量,那么所有文件都会相互覆盖,最后你会得到最后一个文件。

另外我建议你尝试以下逻辑 -

    var fileName = Path.GetExtension(file.FileName);
    string currentDate = DateTime.Now.ToString("yyyyMMddHHmmssfff");

    // You can change this string.format as per your requirement
    // Probably you can use Guid here with proper format.
    string result = String.Format("{0}_{1}",currentDate, fileName);
    var path = Path.Combine((Server.MapPath("~/adpear\\") + storageLoc + "\\" + theUserCompany + "\\" + serial), (serial + "_" + result));
    file.SaveAs(path);
    theSavedImageCount++;

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多