在c#中使用内置FTP客户端上传大文件 [英] Uploading large files in using in a built in FTP client in c#

查看:104
本文介绍了在c#中使用内置FTP客户端上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我在下面有这个代码可以很好地将文件从服务器上传到另一个,但我担心如果客户端有一个大文件到FTP,我正在寻找一些代码来实现缓冲/流媒体的想法文件块而不是一次性FTP文件,我想改变的代码行是requestStream.Write(fileContents,0,fileContents.Length);这是我需要循环并将文件以块的形式发送到FTP服务器的地方,有关如何操作的任何想法吗?



// I have this code below that does fine for uploading file from a server to another, but I am concerned if the client have a large file to FTP, I am looking for some code to implement the idea of buffering/streaming chunks of the file instead of FTPing the file in one shot, the line of code that I am thinking to change is "requestStream.Write(fileContents, 0, fileContents.Length);" This is where I need to loop through and send the file in chunks to FTP server, any ideas on how to do that?

FtpWebRequest request = null;
StreamReader sourceStream = null;
Stream requestStream = null;
FtpWebResponse response = null;







try
          {


//获取用于与服务器通信的对象。

// Get the object used to communicate with the server.

request = (FtpWebRequest)WebRequest.Create(@ftpServer + tmpFilename);
              request.Method = WebRequestMethods.Ftp.UploadFile;
              request.UsePassive = true;
              request.EnableSsl = true;



//此示例假设FTP站点使用匿名登录。


// This example assumes the FTP site uses anonymous logon.

request.Credentials = new NetworkCredential(ftpUser, connectionStringToFTP);

//将文件内容复制到请求流。

// Copy the contents of the file to the request stream.

sourceStream = new StreamReader(@fromSource + tmpFilename);
             byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
             request.ContentLength = fileContents.Length;

             requestStream = request.GetRequestStream();
             requestStream.Write(fileContents, 0, fileContents.Length);

             if (requestStream != null)
             {
                 requestStream.Flush();
                 requestStream.Close();
                 requestStream.Dispose();
             }

             response = (FtpWebResponse)request.GetResponse();
             bool ftpSuccessful = false;

               if (response.StatusDescription.StartsWith("226"))
               {
                   ftpSuccessful = true;
               }
               else
               {

// .....等< br $>

//.....etc.

}
          }
catch(...)
{

//捕获需要捕获的内容......等等。

//catch something required to be captured....etc.

}
finally
{

//关闭/处理/刷新流这里

//close/dispose/flush streams here

}

推荐答案

看看这个:

http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload -file-to-FTP-Server.html [ ^ ]



***编辑***
此处还有一篇文章您可以使用简单C#FTP类 [ ^ ]
Take a look at this:
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html[^]

*** EDIT ***
There is also a article written here with a class you can use Simple C# FTP Class[^]


FTP不支持chunk并行上传只有并行块下载。

但FTP扩展 RFC 3659 [< a href =http://tools.ietf.org/html/rfc3659target =_ blanktitle =新窗口> ^ ]引入了REST命令以恢复损坏的传输。据我所知,FtpWebRequest支持此扩展, ContentOffset [ ^ ]在那里,但是请注意,服务器也必须支持它!

你也可以实现整个协议,在这里你有一篇好文章:http://www.dreamincode.net/forums/topic/35902-create-an-ftp-class-library -in -c%23 / [ ^ ]
FTP does not support chunk parallel upload only parallel chunk download.
But FTP extension RFC 3659[^] introduced the REST command to resume broken transfer. As I know FtpWebRequest is supporting this extension, ContentOffset[^] is there, but be aware, that the server also has to support it!
You can also implement the whole protocol, and here you have a good article: http://www.dreamincode.net/forums/topic/35902-create-an-ftp-class-library-in-c%23/[^]


这篇关于在c#中使用内置FTP客户端上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆