将大文件上载到ftp C#
我正在使用此代码将大文件上载到ftp站点 代码 有时这段代码会很好地隐藏文件,但有时它会隐藏文件的某些部分,而不是完整的文件,并且不会给出任何错误将大文件上载到ftp C#,c#,ftp,C#,Ftp,我正在使用此代码将大文件上载到ftp站点 代码 有时这段代码会很好地隐藏文件,但有时它会隐藏文件的某些部分,而不是完整的文件,并且不会给出任何错误 有谁能帮我解释一下,为什么有时候它只上传文件的一部分而不上传洞文件。下面是我们上传到FTP的代码。它看起来很像你自己的。尽管如此,我还是把它贴出来供你们参考,因为我们还没有收到任何这样的失败报告 private void UploadFile(string fileToUpload) { Output = new Ur
有谁能帮我解释一下,为什么有时候它只上传文件的一部分而不上传洞文件。下面是我们上传到FTP的代码。它看起来很像你自己的。尽管如此,我还是把它贴出来供你们参考,因为我们还没有收到任何这样的失败报告
private void UploadFile(string fileToUpload)
{
Output = new Uri(Path.Combine(Output.ToString(), Path.GetFileName(fileToUpload)));
FtpWebRequest request = WebRequest.Create(Output) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.UploadFile;
// in order to work with Microsoft Windows Server 2003 + IIS, we can't use passive mode.
request.UsePassive = false;
request.Credentials = new NetworkCredential(username, password);
// Copy the contents of the file to the request stream.
Stream dest = request.GetRequestStream();
FileStream src = File.OpenRead(fileToUpload);
int bufSize = (int)Math.Min(src.Length, 1024);
byte[] buffer = new byte[bufSize];
int bytesRead = 0;
int numBuffersUploaded = 0;
do
{
bytesRead = src.Read(buffer, 0, bufSize);
dest.Write(buffer, 0, bufSize);
numBuffersUploaded++;
}
while (bytesRead != 0);
dest.Close();
src.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (response.StatusCode != FtpStatusCode.ClosingData)
{
Console.Error.WriteLine("Request {0}: Error uploading file to FTP server: {1} ({2})", Id, response.StatusDescription, response.StatusCode);
}
else
{
Console.Out.WriteLine("Request {0}: Successfully transferred file to {1}", Id, Output.ToString());
}
}
下面是我们用来上传到FTP的代码。它看起来很像你自己的。尽管如此,我还是把它贴出来供你们参考,因为我们还没有收到任何这样的失败报告
private void UploadFile(string fileToUpload)
{
Output = new Uri(Path.Combine(Output.ToString(), Path.GetFileName(fileToUpload)));
FtpWebRequest request = WebRequest.Create(Output) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.UploadFile;
// in order to work with Microsoft Windows Server 2003 + IIS, we can't use passive mode.
request.UsePassive = false;
request.Credentials = new NetworkCredential(username, password);
// Copy the contents of the file to the request stream.
Stream dest = request.GetRequestStream();
FileStream src = File.OpenRead(fileToUpload);
int bufSize = (int)Math.Min(src.Length, 1024);
byte[] buffer = new byte[bufSize];
int bytesRead = 0;
int numBuffersUploaded = 0;
do
{
bytesRead = src.Read(buffer, 0, bufSize);
dest.Write(buffer, 0, bufSize);
numBuffersUploaded++;
}
while (bytesRead != 0);
dest.Close();
src.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (response.StatusCode != FtpStatusCode.ClosingData)
{
Console.Error.WriteLine("Request {0}: Error uploading file to FTP server: {1} ({2})", Id, response.StatusDescription, response.StatusCode);
}
else
{
Console.Out.WriteLine("Request {0}: Successfully transferred file to {1}", Id, Output.ToString());
}
}
“大”有多大?同样-您是否拥有上载文件的服务器,或者是否有第三方?文件大小高达2 Gb。我使用的是第三方ftp站点。那么第三方是否可能正在断开/关闭连接,或者(如果他们正在移动文件)在文件完成之前将其拾取?您可以改用WebClient.UploadFile()便利方法。这不太可能有什么区别,你几乎肯定要在线路的另一端寻找问题。你说第三方可能会断开/关闭连接,但在这种情况下,它必须给出错误,但没有给出任何错误。我已将缓冲区大小
8092
更改为9999999
,并且它开始上传。我认为当fs.Read
从当时没有任何字符的数据块读取数据时,它会终止重拨和写入。是否有其他方法循环文件结尾。“大”有多大?此外,您是否拥有上载文件的服务器,或者是否有第三方?文件大小高达2 Gb。我使用的是第三方ftp站点。那么第三方是否可能正在断开/关闭连接,或者(如果他们正在移动文件)在文件完成之前将其拾取?您可以改用WebClient.UploadFile()便利方法。这不太可能有什么区别,你几乎肯定要在线路的另一端寻找问题。你说第三方可能会断开/关闭连接,但在这种情况下,它必须给出错误,但没有给出任何错误。我已将缓冲区大小8092
更改为9999999
,并且它开始上传。我认为当fs.Read
从当时没有任何字符的数据块中读取数据,它会终止重读和写入。是否有其他方法循环文件结尾。在您的代码中,它在bytesRead!=0
。当bytesRead=0
时,它离开循环并停止传输。并显示已成功传输。代码中也有同样的内容,它在bytesRead!=0
。当everbytesRead=0
时,它离开循环并停止传输。并显示已成功传输。