从xmlhttprequest到wcf的大文件上传在2分钟后停止

时间:2018-09-20 12:44:20

标签: javascript .net wcf xmlhttprequest

我正在从Chrome将文件上传到自托管的WCF服务,并且2分钟后文件上传停止。再过2分钟后,xhr.send调用onreadystatechange,其无意义的状态为0。

浏览器代码:

   var xhr = new XMLHttpRequest();
   xhr.open('POST', '/upload?filename=' + encodeURIComponent(fileToUpload.serverFilename), true);

   xhr.upload.onprogress = function (e) {
        if (e.lengthComputable) {
            progress.setValue(e.loaded / e.total);
        }
    };

    xhr.onreadystatechange = function () {
        $.unblockUI();
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                if (onSuccess !== undefined) onSuccess(fileToUpload);
            } else {
                alert('Upload Error', 'File: ' + fileToUpload.filename + '<br><bror: ' + xhr.status + ' ' + xhr.statusText);
                if (onError !== undefined) onError();
            }
        }
    };

    xhr.ontimeout = function () {
    // this is not getting called
        console.log('transfer timed out')
    }

    xhr.send(fileToUpload.file);

服务器托管:

mzServerHost = New ServiceHost(GetType(WebAppServer))

Dim zHttpAddress As New Uri("http" & If(mUseHttps, "s", "") & "://" & mUrl & "/")

Dim zContract As Type = GetType(IWebAppServer)

Dim binding As New WebHttpBinding
binding.MaxReceivedMessageSize = 200000000

' 10 minute timeout to allow large file transfers - doesn't work
binding.OpenTimeout = New TimeSpan(0, 10, 0)
binding.CloseTimeout = New TimeSpan(0, 10, 0)
binding.ReceiveTimeout = New TimeSpan(0, 10, 0)
binding.SendTimeout = New TimeSpan(0, 10, 0)

Dim zHttpEndpoint As ServiceEndpoint = mzServerHost.AddServiceEndpoint(zContract, binding, zHttpAddress)
zHttpEndpoint.Behaviors.Add(New WebHttpBehavior)
mzServerHost.Open()

合同:

<OperationContract()>
<DataContractFormat>
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.Bare, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="upload?filename={filename}")>
Function Upload(ByVal filename As String, ByVal file As Stream) As String

上传功能:

   Public Function Upload(ByVal filename As String, file As Stream) As String Implements IWebAppServer.Upload
         Try
            session = WebApp.Sessions.GetSession()
            UserCallContext.WebSession = session
            session.LastCall = Now

            Dim filepath As String = Path.Combine(session.Filestore, filename)

            Using writer As New FileStream(filepath, FileMode.Create)
                Dim buffer(8192) As Byte
                Dim readCount As Integer = file.Read(buffer, 0, buffer.Length)
                Do While readCount <> 0
                    writer.Write(buffer, 0, readCount)
                    readCount = file.Read(buffer, 0, buffer.Length)
                Loop
            End Using

            Return filepath
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
            Throw ex
        End Try
    End Function

我在服务器上设置了WCF超时,尽管2分钟似乎不是默认值,但无济于事。

是什么导致2分钟后停止上传,然后又导致2分钟后调用xhr.onreadstatechange?

测试文件约为50MB,并且在本地计算机上托管时可以正常工作(并在几秒钟内传输)。这只是一个问题,当它托管在网络连接速度较慢(传输停止时完成传输的3/4)的远程计算机上时

0 个答案:

没有答案