XmlHttpRequest上传后暂停使用 [英] XmlHttpRequest Upload Hangs After Pause Between Uses
问题描述
var fileToUpload = document.getElementById(fileInputID).files [0];
var formData = new FormData();
formData.append(file,fileToUpload);
formData.append(queueForUpdate,self.queueForUpdate())
xhr = new XMLHttpRequest();
xhr.open(POST,url,true);
xhr.addEventListener(progress,uploadProgressOnChange,false);
xhr.addEventListener(load,uploadOnLoad,false);
xhr.send(formData);
这是我在失败开始时得到的结果:
> HTTP / 1.1 401 Unauthorized Content-Type:text / html; charset = us-ascii
>服务器:Microsoft-HTTPAPI / 2.0 WWW-Authenticate:协商
> oYHkMIHhoAMKAQGhDAYKKwYBBAGCNwICCqKBywSByE5UTE1TU1AAAgAAAAYABgA4AAAAFcKJ4kDoPTPX1ToPEGzyAQAAAACKAIoAPgAAAAYBsR0AAAAPSQBTAEkAAgAGAEkAUwBJAAEAFABEAEIATwBSAE8AUwBTAC0AVwA3AAQAEgBpAHMAaQBkAGMALgBjAG8AbQADACgAZABiAG8AcgBvAHMAcwAtAHcANwAuAGkAcwBpAGQAYwAuAGMAbwBtAAUAEgBpAHMAaQBkAGMALgBjAG8AbQAHAAgAgtoTc21EzwEAAAAA
个日期:2014年3月20日18:51:48 GMT内容长度:341代理支持:
>基于会话的身份验证
>
> <!DOCTYPE HTML PUBLIC - // W3C // DTD HTML
> 4.01 // ENhttp://www.w3.org/TR/html4/strict.dtd> < HTML>< HEAD>< TITLE>未授权< / title> < META
> HTTP-EQUIV =Content-TypeContent =text / html;
> charset = us-ascii>< / HEAD> < BODY>< h2>未经授权< / h2> < hr>< p> HTTP
>错误401.请求的资源需要用户身份验证。< / p>
> < / BODY>< / HTML>
我已经尝试添加 xhr.withCredentials = true; $ c $没有变化。我正在使用本地IIS Web服务器并从Visual Studio 2012运行代码。
$ b 这不是一个跨站点请求。
在我的设置中,我会收到401回来,IE似乎回应与凭据和文件上传的服务器。无论出于何种原因,服务器都没有响应该请求,然后超时。
我在其他StackOverflow问题和意见中找到了一个建议。建议先发送一个GET请求,并在发送文件上传前进行身份验证。这对我有效。这里是我的代码执行的步骤:
$ b $ ol <
欢迎任何评论或解释。
I am working on a single page application with a file upload portion. Knockout is used to bind the view to a viewmodel. This application is an intranet application using Windows Authentication and IE10 as the client. If the user initially uploads a file, all is well. If, after 1 upload, there is a 1 minute or more pause and another upload is attempted, the application will hang. Here is my file upload code:
var fileToUpload = document.getElementById(fileInputID).files[0];
var formData = new FormData();
formData.append("file", fileToUpload);
formData.append("queueForUpdate", self.queueForUpdate())
xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.addEventListener("progress", uploadProgressOnChange, false);
xhr.addEventListener("load", uploadOnLoad, false);
xhr.send(formData);
This is what I get back when the failures start:
> HTTP/1.1 401 Unauthorized Content-Type: text/html; charset=us-ascii
> Server: Microsoft-HTTPAPI/2.0 WWW-Authenticate: Negotiate
> oYHkMIHhoAMKAQGhDAYKKwYBBAGCNwICCqKBywSByE5UTE1TU1AAAgAAAAYABgA4AAAAFcKJ4kDoPTPX1ToPEGzyAQAAAACKAIoAPgAAAAYBsR0AAAAPSQBTAEkAAgAGAEkAUwBJAAEAFABEAEIATwBSAE8AUwBTAC0AVwA3AAQAEgBpAHMAaQBkAGMALgBjAG8AbQADACgAZABiAG8AcgBvAHMAcwAtAHcANwAuAGkAcwBpAGQAYwAuAGMAbwBtAAUAEgBpAHMAaQBkAGMALgBjAG8AbQAHAAgAgtoTc21EzwEAAAAA
> Date: Thu, 20 Mar 2014 18:51:48 GMT Content-Length: 341 Proxy-Support:
> Session-Based-Authentication
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
> 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <HTML><HEAD><TITLE>Not Authorized</TITLE> <META
> HTTP-EQUIV="Content-Type" Content="text/html;
> charset=us-ascii"></HEAD> <BODY><h2>Not Authorized</h2> <hr><p>HTTP
> Error 401. The requested resource requires user authentication.</p>
> </BODY></HTML>
I have tried adding xhr.withCredentials = true;
with no change. I am using the local IIS web server and running the code from Visual Studio 2012.
This is not a cross site request.
This issue seems to stem from the way Internet Explorer works with NTLM authentication. IE detects that a site is using NTLM authentication when it receives HTTP 401 responses from the domain. When a large POST operation like the one in my question occur, IE will send a zero byte POST request expecting to get a 401 back which it can then respond to with the user credentials. This prevents the data having to be sent twice.
In my setup I would receive the 401 back and IE seemed to respond to the server with the credentials and the file upload. For whatever reason, the server did not respond to that request which would then timeout.
I found a suggestion in other StackOverflow questions and comments. It was suggested to issue a GET request first and let it authenticate before sending the file upload. This worked for me. Here are the steps my code performs:
- Send a HTTP GET request to the server. I created a NOP (no operation) action on my ASP.NET MVC controller for this.
- When the server responds to the GET request, perform the file upload call. There doesn't appear to be any challenge/response in this event. The file is sent to the server and the server responds accordingly.
Any comments or explanations would be welcome.
这篇关于XmlHttpRequest上传后暂停使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!