vb.net XMLHttpRequest ::ERR_CONNECTION_RESET 上传大型(2 Mo 及更多)文件时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27428375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest ::ERR_CONNECTION_RESET while uploading large (2 Mo & More) files
提问by Simon Dugré
Problem
问题
I'm trying to upload file using XMLHttpRequest and it seems to work only with small file (such as under 2MO of size). I tried many things so far and came to code shown at the end of the post. But, there is nothing to do; I keep getting the ::ERR_CONNECTION_RESETerror. It is not a code issue as under 2MO files are getting unploaded correctly... What am I forgetting? I know this is probably a IIS or web.config issues but I can put my finger on it by googling this problem.
我正在尝试使用 XMLHttpRequest 上传文件,它似乎只适用于小文件(例如小于 2MO 的大小)。到目前为止,我尝试了很多东西,并最终找到了帖子末尾显示的代码。但是,没有什么可做的;我不断收到::ERR_CONNECTION_RESET错误。这不是代码问题,因为 2MO 下的文件正在正确卸载......我忘记了什么?我知道这可能是 IIS 或 web.config 问题,但我可以通过谷歌搜索这个问题来解决它。
Error given by Chrome
Chrome 给出的错误
POST WEBSITEANDSERVICEURL/Service/MyService.asmx/UploadFilesWithAJAXnet::ERR_CONNECTION_RESET
- handleFileSelect
- x.event.dispatch
- v.handle
POST WEBSITEANDSERVICEURL/Service/MyService.asmx/UploadFilesWithAJAXnet::ERR_CONNECTION_RESET
- 处理文件选择
- x.event.dispatch
- v.句柄
Javascript
Javascript
<script type="text/javascript">
$(function() {
$('#<%= files.ClientID %>').change(handleFileSelect);
});
function handleFileSelect(e) {
if (!e.target.files || !window.FileReader) return;
var fd = new FormData();
var file = document.getElementById("<%= files.ClientID %>");
for (var i = 0; i < file.files.length; i++) {
fd.append('_file', file.files[i]);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '<%= ResolveURL("~/Services/MyService.asmx/UploadFilesWithAJAX") %>', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
xhr.upload.addEventListener("progress", updateProgress, false);
xhr.send(fd);
}
function updateProgress(oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = oEvent.loaded / oEvent.total;
$("#progress").text(oEvent.loaded + " ON " + oEvent.total);
}
}
</script>
HTML Markup
HTML 标记
<asp:FileUpload ID="files" runat="server" multiple="true" />
<br />
<table id="selectedFiles">
</table>
<span id="progress"></span>
MyService.asmx
我的服务.asmx
<ScriptService()> _
<ToolboxItem(False)> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class AJAXServices : Inherits WebService
<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Xml)> _
Public Function UploadFilesWithAJAX()
' Some code which work fine, I'm sure of it.
End Function
End Class
Web.config
网页配置
<!-- [...] -->
<system.web>
<httpRuntime maxRequestLength="2097151" executionTimeout="180" /><!-- MAXIMUM DURING TESTING -->
<!-- [...] -->
</system.web>
<!-- [...] -->
采纳答案by Simon Dugré
Ok, I solved it...
好的,我解决了...
Solution
解决方案
If this happen to someone else, be sure to at least access to Context.Request.Filesonce in your WebService.
如果其他人发生这种情况,请确保至少Context.Request.Files在您的 WebService 中访问一次。
Because, during my tests, I was simply :
因为,在我的测试中,我只是:
Public Function UploadFilesWithAJAX()
Return "RETURN SOMETHING"
End Function
But it was not enough... If I only access to Context.Request.Files like :
但这还不够......如果我只访问 Context.Request.Files 像:
Public Function UploadFilesWithAJAX()
Dim files = Context.Request.Files '<---- Simply adding this... make it works :|
Return "RETURN SOMETHING"
End Function
It works. Hope it helps someone else.
有用。希望它可以帮助别人。
By the way, if someone can explain me why it is working by doing this.
顺便说一句,如果有人可以通过这样做来解释我为什么这样做。
回答by Ercan
In your web config, you defined maxRequestLength="2097151" which is around 2mb, for this reason if you try to upload files more than 2mb it will fail eventually.
在您的网络配置中,您定义了 maxRequestLength="2097151" 大约 2mb,因此,如果您尝试上传超过 2mb 的文件,它最终会失败。
Example config below( it will allow up to 2gb )
下面的示例配置(最多允许 2gb)
<httpRuntime maxRequestLength="2048576000" executionTimeout="300" />

