3

我正在尝试将一个大文件(超过 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

4

3 回答 3

1

我注意到您没有关闭输入文件流(fs)。我有一个 FTP 文件然后将其移动到“成功”文件夹的过程。因为 fs 没有关闭,所以当我试图移动它时,我得到了一个“正在被另一个进程使用的文件”。我在 Try-catch 中抓住了它。我不确定是不是这样,因为你说 50MB 文件成功,但这是一个想法。

我将放入fs.close()并查看是否可以解决我的错误。我移动了一个 189MB 的文件,我使用的连接需要一些时间......但它似乎在移动之前一直有效。

于 2013-03-01T22:40:34.990 回答
0

那么,文件本身是否到达了 FTP?如果是这样,解决方法是在给定的时刻中断该过程,然后保持不变。

您是否尝试过摆弄设置?

于 2011-08-30T06:20:24.907 回答
0

我认为您可能只需要增加超时时间。

在循环之外放置一个Try-Catch循环来捕获返回的错误,您可能会发现错误是:

底层连接已关闭

希望有帮助!

于 2012-04-10T16:32:01.130 回答