简体   繁体   English

MVC 4-上载到IIS 7.5后在IE中不起作用

[英]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) 仅当我将其部署到实时服务器上时,才会出现此问题,它在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中结果为file.ContentLength = 0,但在FF和Chrome中工作正常,如果有任何区别,则该机器位于我们的Intranet上。

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的答案标记为正确的答案,因为他的职位将我引向正确的答案/解释。

Your issue has nothing to do with the IIS. 您的问题与IIS无关。 It is broken because you are wiring up using the change event. 它已断开,因为您正在使用change事件进行连接。 There are known issues with IE and the change event. IE和change事件存在已知问题。 A table with the compatibility of different browsers with this event on different HTML elements is available at quirksmode . 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. 对我而言,最明显的方法是使用blurfocus方法。 (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; } 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? 此外,您是否已确认该功能可在没有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. 如果不是这样,那么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. 在对您的帖子的评论中,您说您已在maxRequestLength中启用了2GB。 However, this works a little differently in IIS7 than in IIS6 (or Casini if that was your previous test environment). 但是,此方法在IIS7中的工作方式与在IIS6中的工作方式稍有不同(如果是以前的测试环境,则为Casini)。 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. 有关详细信息,请参见本博客 ,但总而言之,您需要确保web.config在system.webServer部分中具有该设置,并且该值实际上代表您的想法。 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? 是否有任何空的try - catch块可能会保护您的视图免受潜在错误的影响,否则可能导致请求失败? 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. 我注意到您有一个硬编码的表单操作/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. 您在评论中说内容长度为0,但是如果您用来确定它的测试是帖子中的内容,则您可能是错误的。 Your test is equivalent to file == null || file.ContentLength == 0) 您的测试等效于file == null || file.ContentLength == 0) 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. 如果内容长度为0,则它​​至少识别出正在发送文件,但是在决定如何处理时出现了问题。 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). 您可以通过遍历HttpContext.Request.Form集合并写出值来检查是否真正接收到文件数据(这就是ASP Classic时代的做法)。 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. 一篇文章, Valum文件上载-在Chrome中工作,但在IE中不工作,Image img = Image.FromStream(Request.InputStream)表示,具有相同问题的用户在Request.Files集合中找到了文件内容。 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). 我还在Telerik论坛上发现了一些东西,这些东西似乎描述了与它们的组件相似的事情(实际上只是您所做工作的包装)。 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. 上一次,当我编写基于Ajax的UI时遇到同样的问题。我使用ajax上传文件。

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(Request.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 ? 您是通过ajax还是通过表单提交来保存postFile()文件? IE not take the filename (of upload) as chrome and firefox does. IE不会像chrome和firefox那样使用(上传的)文件名。 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 . 在IE11上为我工作的简单解决方案: 启用兼容模式

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. 出于某种原因,IE11对我的上载端点执行了两个请求...一个不带文件,可以正常工作并返回200状态(但是由于它没有文件,因此对我无用),另一个请求与文件...但是第二个总是在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. 但是,这并不能解决每个人的问题,因为您必须要求系统的每个用户都为您的网站启用兼容模式...而且向Internet用户提出要求不是一件好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC应用程序中的$ .get()和$ .post()在本地Cassini网络服务器中可以正常工作,但是当发布到IIS 7.5中后,它将不再起作用 - $.get() and $.post() in MVC app works fine in local Cassini webserver but when published into IIS 7.5 it doesn't work anymore 部署在IIS 7.5上的MVC应用程序不写入日志文件 - MVC app not writing log files when deployed on IIS 7.5 MVC4部署到远程IIS 7.5,访问sql server时出现401.2错误 - MVC4 deployed to remote IIS 7.5, gives 401.2 error when accessing sql server 将mvc项目部署到Win Server 2008 R2后,IE 9边界半径不起作用 - IE 9 border-radius doesn't work after mvc project is deployed to Win server 2008 r2 尽管有WWWfilter,但已部署IIS7的MVC仍未重定向 - IIS7 deployed MVC doesn't redirect despite WWWfilter IIS 7.5 MVC 5部署的网站将无法连接到SQL Server - IIS 7.5 MVC 5 deployed website will not connect to SQL Server ASP.Net MVC2-IIS7.5找不到主页的查看文件 - ASP.Net MVC2 - IIS7.5 doesn't find view file for home page 带有Selenium的C#.NET-MVC应用程序在服务器上部署时不起作用 - C# .NET - MVC app with Selenium doesn't work when deployed on Server 无法显示部署到IIS 8页面的MVC 4 - MVC 4 deployed to IIS 8 Page can't be Displayed 部署在远程服务器上时,MVC应用程序不发送电子邮件 - MVC app doesn't send email when deployed on a remote server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM