XMLHttpRequest ::ERR_CONNECTION_RESET 同时上传大型(2 Mo & More

XMLHttpRequest ::ERR_CONNECTION_RESET while uploading large (2 Mo & More) files

本文关键字:Mo More 大型 amp CONNECTION ERR RESET XMLHttpRequest      更新时间:2023-09-26

>问题

我正在尝试使用 XMLHttpRequest 上传文件,它似乎仅适用于小文件(例如大小低于 2MO)。到目前为止,我尝试了很多东西,并得到了帖子末尾显示的代码。但是,无事可做;我不断收到::ERR_CONNECTION_RESET错误。这不是代码问题,因为在 2MO 下文件被正确卸载......我忘记了什么?我知道这可能是 IIS 或 web.config 的问题,但我可以通过谷歌搜索这个问题来解决这个问题。

铬给出的错误

POST WEBSITEANDSERVICEURL/Service/MyService.asmx/UploadFilesWithAJAX net::ERR_CONNECTION_RESET

  • 句柄文件选择
  • x.事件.调度
  • 五、把手

爪哇语

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

网页标记

<asp:FileUpload ID="files" runat="server" multiple="true" />
<br />
<table id="selectedFiles">
</table>
<span id="progress"></span>

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

网络.config

<!-- [...] -->
<system.web>
    <httpRuntime maxRequestLength="2097151" executionTimeout="180" /><!-- MAXIMUM DURING TESTING -->
    <!-- [...] -->
</system.web>
<!-- [...] -->

好的,我解决了...

溶液

如果这种情况发生在其他人身上,请确保至少在您的 Web 服务中访问一次Context.Request.Files

因为,在我的测试中,我只是:

Public Function UploadFilesWithAJAX()
    Return "RETURN SOMETHING"
End Function

但这还不够...如果我只访问 Context.Request.Files,例如:

 Public Function UploadFilesWithAJAX()
    Dim files = Context.Request.Files '<---- Simply adding this... make it works :|
    Return "RETURN SOMETHING"
End Function

它有效。希望它对其他人有所帮助。

顺便说一句,如果有人可以通过这样做来解释我为什么它会起作用。

在您的 Web 配置中,您定义了 maxRequestLength="2097151",大约是 2mb,因此,如果您尝试上传超过 2mb 的文件,它最终会失败。

下面的示例配置(它将允许最多 2gb )

<httpRuntime maxRequestLength="2048576000" executionTimeout="300" />