Question

I'm using MVC4 on IIS8.0 to POST multiple image files to a server. This has been working for a long time, but recently the application needed to change where it saved the images. Before, I was saving to a Network location, and now I am saving directly to the same webserver where the application is running.

Now when I upload only one file gets saved. Specifically the last file in the list.

Why is this now happening? Also, I can run the application on localhost and it will save all images to the web server just fine. Once I publish and try it, it starts only taking the final image again.

From 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>

Here are a few of the lines from web.config.

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

From the Controller.

[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++;
        }
    }
}
Was it helpful?

Solution

Check the path variable on the server, i think it is same for all the files. If you have the same path variable, then all files will override one another and finally you end up with last file.

Also I would suggest you to try the following logic -

    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++;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top