MVC 4 -上载在IE中部署到IIS 7.5时不能工作

时间:2022-12-02 11:25:54

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)

这个问题只有在我将它部署到我们的live服务器时才会出现,它在我的visual studio web服务器的开发机器上运行良好,而且在IE中也只是一个问题(8和9都是)

Posting a jpeg with this form...

使用此表格张贴jpeg文件…

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

使用这个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.

结果文件。在IE中,内容长度为0,但是在FF和Chrome中,它工作得很好。

Any help greatly appreciated

任何帮助深表感谢

UPDATE 1:

更新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.

奇怪的是问题似乎是断断续续的,周五我的同事不能上传任何东西,但是我可以,今天早上是我不能,我的同事可以同时使用IE。

Everything seems to point to an IIS config problem?

一切似乎都指向IIS配置问题?

UPDATE 2:

更新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.

主站点使用表单身份验证,但是我有另一个站点为我做windows身份验证并设置cookie,禁用此功能为我修复了这个问题。

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.

我认为smartcaveman的答案是正确的,因为他的文章让我找到了正确的答案/解释。

2 个解决方案

#1


1  

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.

您的问题与IIS无关。它被破坏,因为您正在使用更改事件连接。IE和更改事件存在已知的问题。在quirksmode网站上可以找到一个具有不同浏览器与此事件在不同HTML元素上的兼容性的表。

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).

我想到的最明显的方法是使用blur和focus方法。(在焦点上记录一个值,在blur中检查,如果提交不同)。

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. e.g. 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? (e.g. 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.

    如果您试图在提交之前从客户端的表单字段中获取值,会发生什么情况。例如:函数postFile(ctrl) {alert(document.getElementById('uploadForm').value);返回错误;}。它有价值吗?另外,您是否已确认该特性在没有JavaScript的实时环境中工作?(例如:正常递交表格)。如果是的话,那么你可以确定问题在前端。如果没有,JS很好,问题就在后台。

  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.

    最大请求长度/最大内容长度。在您的帖子的评论中,您说您已经在maxRequestLength中启用了最多2GB。然而,IIS7的工作方式与IIS6的工作方式稍有不同(或者如果这是您以前的测试环境,则是Casini)。在这个博客中,我们引用了这些细节,但总的来说,你需要确保你的网站。配置在系统中有设置。webServer部分,这个值实际上代表了你认为它的功能。细节在这篇文章中。

  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?

    您是否有异常处理和/或日志功能?是否有可能你正在吞下一个导致请求被放弃的异常?是否存在可能保护视图不受潜在错误影响的空try - catch块,否则将导致请求失败?如果您确实启用了日志记录,那么您能将您的尝试使用与您的同事的使用区分开来吗?

  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.

    live站点上的文件结构与开发环境有什么不同吗?我注意到您有一个硬编码的表单动作/ImageUpload/UploadImageNew目标,这可能会影响应用程序匹配传入路由的能力。

  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). 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).

    您在评论中说内容长度为0,但是如果您用来确定这是您在文章中所做的测试,那么您可能是错的。您的测试相当于文件= null ||文件。内容长度== = 0)根据实际情况的不同有不同的含义。如果文件为空,那么它可能与模型绑定相关。如果内容长度为0,那么它至少可以识别正在发送的文件,但是当它决定如何处理时,就会出现问题。您可以通过HttpContext.Request循环检查是否实际接收文件数据。收集表单并写出值(这是我们以前的做法……)在ASP经典的日子里)。另一篇文章Valum file upload - work in Chrome but not IE, Image img = Image. fromstream (Request. inputstream)说,有相同问题的用户在请求中找到了文件内容。文件收集。在你检查之前你是不会知道的。无论如何,如果文件数据位于其中一个位置,那么您就知道模型绑定中正在发生问题(并且您知道相关的工作,直到找到正确的修复方法)。

  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.

    我还在Telerik论坛上找到了一些类似的东西,它们的组件(实际上就是你正在做的事情的包装)也会发生类似的事情。它指出了一个可能的会话时间。

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

我建议仔细检查这些项目,看看其中是否有什么可以揭示问题的更多信息(至少第一点是肯定的)。

#2


0  

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.

上次我在编写基于ajax的UI时遇到了同样的问题。我使用ajax上传文件。那时只有Ie制造问题。

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)

Ajax文件上传不能在IE7 Valum文件上传中工作——可以在Chrome中工作但不能在IE中工作,图像img = Image. fromstream(请求。inputstream)

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

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.

您的postFile()是通过ajax保存文件还是通过表单提交?IE不像chrome和firefox那样使用上传文件名。当您上传调试代码并检查它们是否存在时。

#1


1  

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.

您的问题与IIS无关。它被破坏,因为您正在使用更改事件连接。IE和更改事件存在已知的问题。在quirksmode网站上可以找到一个具有不同浏览器与此事件在不同HTML元素上的兼容性的表。

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).

我想到的最明显的方法是使用blur和focus方法。(在焦点上记录一个值,在blur中检查,如果提交不同)。

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. e.g. 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? (e.g. 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.

    如果您试图在提交之前从客户端的表单字段中获取值,会发生什么情况。例如:函数postFile(ctrl) {alert(document.getElementById('uploadForm').value);返回错误;}。它有价值吗?另外,您是否已确认该特性在没有JavaScript的实时环境中工作?(例如:正常递交表格)。如果是的话,那么你可以确定问题在前端。如果没有,JS很好,问题就在后台。

  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.

    最大请求长度/最大内容长度。在您的帖子的评论中,您说您已经在maxRequestLength中启用了最多2GB。然而,IIS7的工作方式与IIS6的工作方式稍有不同(或者如果这是您以前的测试环境,则是Casini)。在这个博客中,我们引用了这些细节,但总的来说,你需要确保你的网站。配置在系统中有设置。webServer部分,这个值实际上代表了你认为它的功能。细节在这篇文章中。

  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?

    您是否有异常处理和/或日志功能?是否有可能你正在吞下一个导致请求被放弃的异常?是否存在可能保护视图不受潜在错误影响的空try - catch块,否则将导致请求失败?如果您确实启用了日志记录,那么您能将您的尝试使用与您的同事的使用区分开来吗?

  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.

    live站点上的文件结构与开发环境有什么不同吗?我注意到您有一个硬编码的表单动作/ImageUpload/UploadImageNew目标,这可能会影响应用程序匹配传入路由的能力。

  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). 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).

    您在评论中说内容长度为0,但是如果您用来确定这是您在文章中所做的测试,那么您可能是错的。您的测试相当于文件= null ||文件。内容长度== = 0)根据实际情况的不同有不同的含义。如果文件为空,那么它可能与模型绑定相关。如果内容长度为0,那么它至少可以识别正在发送的文件,但是当它决定如何处理时,就会出现问题。您可以通过HttpContext.Request循环检查是否实际接收文件数据。收集表单并写出值(这是我们以前的做法……)在ASP经典的日子里)。另一篇文章Valum file upload - work in Chrome but not IE, Image img = Image. fromstream (Request. inputstream)说,有相同问题的用户在请求中找到了文件内容。文件收集。在你检查之前你是不会知道的。无论如何,如果文件数据位于其中一个位置,那么您就知道模型绑定中正在发生问题(并且您知道相关的工作,直到找到正确的修复方法)。

  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.

    我还在Telerik论坛上找到了一些类似的东西,它们的组件(实际上就是你正在做的事情的包装)也会发生类似的事情。它指出了一个可能的会话时间。

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

我建议仔细检查这些项目,看看其中是否有什么可以揭示问题的更多信息(至少第一点是肯定的)。

#2


0  

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.

上次我在编写基于ajax的UI时遇到了同样的问题。我使用ajax上传文件。那时只有Ie制造问题。

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)

Ajax文件上传不能在IE7 Valum文件上传中工作——可以在Chrome中工作但不能在IE中工作,图像img = Image. fromstream(请求。inputstream)

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

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.

您的postFile()是通过ajax保存文件还是通过表单提交?IE不像chrome和firefox那样使用上传文件名。当您上传调试代码并检查它们是否存在时。