我正在尝试将一个大文件(超过 50Mb)上传到我的网络服务器,但我的应用程序在尝试关闭流时挂起。如果上传的文件大于 50Mb,则 .Close() 会导致它挂起 - 根本没有错误消息 - 但是小于 50Mb 的文件会成功。
你有什么建议绕过 fstream.Close() 挂起我的应用程序?
Dim target As New Uri(uploadedFilePath)
Dim fRequest As System.Net.FtpWebRequest = System.Net.WebRequest.Create(target)
fRequest.Credentials = New System.Net.NetworkCredential(usr, pswd)
fRequest.KeepAlive = False
fRequest.Proxy = Nothing
fRequest.UsePassive = True
fRequest.UseBinary = True
fRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
fRequest.Timeout = 180000
Dim count As Integer = 0
Dim readBytes As Integer = 0
Const bufferLength As Integer = 8192
Dim buffer As Byte() = New Byte(bufferLength - 1) {}
Dim fs As FileStream = File.OpenRead(localFileName)
Dim fStream As Stream = fRequest.GetRequestStream
Console.WriteLine("Writing bytes to the stream. {0}", String.Format("{0:HH:mm}", Now))
Do
readBytes = fs.Read(buffer, 0, bufferLength)
fStream.Write(buffer, 0, readBytes)
count += readBytes
Loop While readBytes <> 0
Console.WriteLine("Writing {0} bytes to the stream. {1}", count, String.Format("{0:HH:mm}", Now))
fStream.Close()
Console.WriteLine("fstream Closed {0}", String.Format("{0:HH:mm}", Now))
输出为:
Writing bytes to the stream. 13:08
Writing 51391500 bytes to the stream. 13:18
注意最后一个 Console.Writeline 永远不会输出。
PS 使用 Visual Studio 2010 和 .Net Framework 4.0