简体   繁体   中英

MVC 4 - Upload Doesn't Work in IE When deployed to IIS 7.5

This problem only happens when I deploy this to our live server, it works fine on my development machine in the visual studio web server, and it is only a problem in IE (8 & 9 both)

Posting a jpeg with this form...

<form method="post" id="uploadForm" enctype="multipart/form-data" action="/ImageUpload/UploadImageNew">
   <input type="file" onchange="postFile()" name="file"></div>
   <input type="submit" value="OK">
</form>

Using this javascript...

function postFile(ctrl) {
    document.getElementById('uploadForm').submit();
}

To this controller...

[HttpPost]
public ActionResult UploadImageNew(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0)
    {
        file.SaveAs("AFilename.file");
     }
     else
     {
         throw new Exception("File not found")
     }
     return View("UploadImageNew");
}

Results in file.ContentLength = 0 in IE, but it works fine in FF & Chrome, the machine is on our Intranet if that makes any difference.

Any help greatly appreciated

UPDATE 1:

Weirdly it seems that the problem is intermittent, on Friday my colleague could not upload anything but I could, this morning it is me who can't and my colleague can both using IE.

Everything seems to point to an IIS config problem?

UPDATE 2:

Ok, it looks like my issue is related to expired sessions / security. I am using a Hybrid Authentication Method and it is causing me problems.

The main site uses forms authentication, however I have another site which does windows authentication for me and sets the cookies, disabling this fixed the issue for me.

Not sure why yet but I think this would be better in another question.

I am marking smartcaveman's answer as the right one, because his post lead to me to the right answer/explanation.

Your issue has nothing to do with the IIS. It is broken because you are wiring up using the change event. There are known issues with IE and the change event. A table with the compatibility of different browsers with this event on different HTML elements is available at quirksmode .

Here are a couple of blog articles on the subject:

The most obvious way that occurs to me to do this is to use the blur and focus methods instead. (Record a value on focus, check it on blur, and if it's different submit).

Update

So, It's still not working? Here's a few other things that are worth looking into.

  1. What happens if you try to get the value from the form field on the client-side, prior to submitting. eg function postFile(ctrl) { alert(document.getElementById('uploadForm').value); return false; } function postFile(ctrl) { alert(document.getElementById('uploadForm').value); return false; } function postFile(ctrl) { alert(document.getElementById('uploadForm').value); return false; } . Does it have a value? Also, have you confirmed the feature works on the live environment without the JavaScript? (eg just submit the form normally). If it does, then you can be sure the problem is on the front end. If it doesn't then the JS is fine and the problem is on the back-end.

  2. Max Request Length / Max Content Length. In a comment on your post, you said that you have enabled up to 2GB in the maxRequestLength. However, this works a little differently in IIS7 than in IIS6 (or Casini if that was your previous test environment). The details about this are cited in this blog , but in summary you need to make sure that your web.config has the setting in the system.webServer section, and that the value actually represents what you think it does. Details are in the article.

  3. Do you have exception handling and/or logging features in play? Is it possible you are swallowing an exception somewhere that is causing the request to be abandoned? Are there any empty try - catch blocks that might be protecting your view from an underlying error that would otherwise cause the request to fail? If you do have logging enabled, can you isolate a distinction between your attempted usage and your colleague's?

  4. Is there anything different about the file structure on the live site than your development environment? I noticed you have a hard-coded form action /ImageUpload/UploadImageNew target, which could affect the application's ability to match the incoming route.

  5. You said in the comment that the content length is 0, but if the test you used to determine this is what you have in the post, then you may be wrong. Your test is equivalent to file == null || file.ContentLength == 0) file == null || file.ContentLength == 0) . There are different implications depending on which is actually the case. If the file is null, then it may related to model binding. If the Content length is 0, then it at least recognized a file is being sent, but something goes wrong when it decides what to do about it. You can check to see if you are actually receiving the file data by looping through the HttpContext.Request.Form collection and writing out the values (That's how we used to do it... in the ASP Classic days). This other post, Valum file upload - Works in Chrome but not IE, Image img = Image.FromStream(Request.InputStream) , says that a user with the same kind of issue found the file content in the Request.Files collection. You won't know for sure until you check. Anyway, if the file data is in one of those places, you know that problem is happening in model binding (and you know the work around until you find the right way to fix it).

  6. I also found something on the Telerik forums that seems to describe something similar that happens with their component (which is really just a wrapper of what you are doing). It points to a possible session time out.

I would suggest going through these items and seeing if any of them reveal more about the problem (at least #1 is sure to).

Last time I have the same problem when I write ajax based UI.I upload the file using the ajax.only Ie create problem for that time.

Not sure but try these link maybe helpful

Ajax file upload not working in IE7 Valum file upload - Works in Chrome but not IE, Image img = Image.FromStream(Request.InputStream)

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

Is you postFile() save file by ajax or form submission ? IE not take the filename (of upload) as chrome and firefox does. When you make upload debug the code and check if they are exist.

The easy solution that worked for me on IE11: Enable Compatibility mode .

For some reason, IE11 was doing two requests to my Upload endpoint... One without the file, that works fine and returns 200 status (but since it doesn't has the file, it's useless for me) and another request with the file... But this second one is always "pending" on the network tab of the Developer Tools inside IE. I couldn't find the reason for this odd behavior. Watching the logs from server I noticed this second request never hit the server.

This however don't solve the problem for everyone since you'll have to ask every user of your system to enable compatibility mode for your website... And it's not a good thing to ask for Internet users.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM