VB.net webrequest.uploadfile 中止错误
VB.net webrequest.uploadfile aborted error
我使用 webrequest 上传文件功能。但是我在慢速网络中遇到大文件错误。所以我想也许缓冲区或其他东西可以帮助我
我的代码
Private Shared Function GetFileUploadResponse(ByVal fileToUpload As String, ByVal accessToken As String, ByVal uploadUrl As String) As UploadResponse
Dim client = New WebClient()
client.Headers.Add("Authorization", "OAuth " & accessToken)
Dim responseBytes = client.UploadFile(uploadUrl, fileToUpload)
Dim responseString = Encoding.UTF8.GetString(responseBytes)
Dim response = JsonConvert.DeserializeObject(Of UploadResponse)(responseString)
Return response
End Function
当我尝试上传更大的 100 MB 文件时错误中止。
我找到了这段代码,但这是针对 FTP 的。我使用普通的网络上传。我尝试修改我的代码,但每次都出错
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
ftpStream As Stream = request.GetRequestStream()
Dim read As Integer
Do
Dim buffer() As Byte = New Byte(10240) {}
read = fileStream.Read(buffer, 0, buffer.Length)
If read > 0 Then
ftpStream.Write(buffer, 0, read)
Console.WriteLine("Uploaded {0} bytes", fileStream.Position)
End If
Loop While read > 0
End Using
你必须在服务器端设置最大文件上传大小,显然你的设置为100mb。
On the server:
Open IIS Manager.
Select the website with your site name.
Double-click on "Request Filtering".
Click "Edit Feature Settings" on the right hand side of the page.
In the dialogue window that opens you can see the "Maximum allowed content length" field. The default is 30mb, apparently yours is set to 100mb Let us say we want our files to be up to 1GB, then we would need to put "1073741824" there.
已找到解决方案。关于 webclient 超时的问题。这个 modifield 网络客户端运行良好。
Public Class BigWebClient
Inherits WebClient
Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim x As WebRequest = MyBase.GetWebRequest(address)
x.Timeout = 60 * 60 * 1000
Return x
End Function
End Class
我使用 webrequest 上传文件功能。但是我在慢速网络中遇到大文件错误。所以我想也许缓冲区或其他东西可以帮助我
我的代码
Private Shared Function GetFileUploadResponse(ByVal fileToUpload As String, ByVal accessToken As String, ByVal uploadUrl As String) As UploadResponse
Dim client = New WebClient()
client.Headers.Add("Authorization", "OAuth " & accessToken)
Dim responseBytes = client.UploadFile(uploadUrl, fileToUpload)
Dim responseString = Encoding.UTF8.GetString(responseBytes)
Dim response = JsonConvert.DeserializeObject(Of UploadResponse)(responseString)
Return response
End Function
当我尝试上传更大的 100 MB 文件时错误中止。
我找到了这段代码,但这是针对 FTP 的。我使用普通的网络上传。我尝试修改我的代码,但每次都出错
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
ftpStream As Stream = request.GetRequestStream()
Dim read As Integer
Do
Dim buffer() As Byte = New Byte(10240) {}
read = fileStream.Read(buffer, 0, buffer.Length)
If read > 0 Then
ftpStream.Write(buffer, 0, read)
Console.WriteLine("Uploaded {0} bytes", fileStream.Position)
End If
Loop While read > 0
End Using
你必须在服务器端设置最大文件上传大小,显然你的设置为100mb。
On the server:
Open IIS Manager.
Select the website with your site name.
Double-click on "Request Filtering".
Click "Edit Feature Settings" on the right hand side of the page.
In the dialogue window that opens you can see the "Maximum allowed content length" field. The default is 30mb, apparently yours is set to 100mb Let us say we want our files to be up to 1GB, then we would need to put "1073741824" there.
已找到解决方案。关于 webclient 超时的问题。这个 modifield 网络客户端运行良好。
Public Class BigWebClient
Inherits WebClient
Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim x As WebRequest = MyBase.GetWebRequest(address)
x.Timeout = 60 * 60 * 1000
Return x
End Function
End Class