XmlHttpRequest上传后暂停使用 [英] XmlHttpRequest Upload Hangs After Pause Between Uses

查看:158
本文介绍了XmlHttpRequest上传后暂停使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文件上传部分处理单个页面的应用程序。 Knockout用于将视图绑定到视图模型。此应用程序是使用Windows身份验证和IE10作为客户端的Intranet应用程序。如果用户最初上传文件,一切都很好。如果在1次上载之后,暂停1分钟或更长时间并尝试另一次上载,则应用程序将挂起。这里是我的文件上传代码:

  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; $ b 这不是一个跨站点请求。


在我的设置中,我会收到401回来,IE似乎回应与凭据和文件上传的服务器。无论出于何种原因,服务器都没有响应该请求,然后超时。

我在其他StackOverflow问题和意见中找到了一个建议。建议先发送一个GET请求,并在发送文件上传前进行身份验证。这对我有效。这里是我的代码执行的步骤:
$ b $ ol <

  • 发送一个HTTP GET请求到服务器。我在我的ASP.NET MVC控制器上创建了一个NOP(无操作)动作。

  • 当服务器响应GET请求时,执行文件上传调用。在这个事件中似乎没有任何挑战/反应。该文件被发送到服务器,服务器响应。

  • 欢迎任何评论或解释。


    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:

    1. Send a HTTP GET request to the server. I created a NOP (no operation) action on my ASP.NET MVC controller for this.
    2. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆