【问题标题】:Progress in uploading in ftp server c#在ftp服务器c#中上传的进度
【发布时间】:2016-03-28 10:30:33
【问题描述】:

我有以下代码

foreach (string dir in dirs) { //dirs all files in directory
    try{
       // Get an instance of WebClient
       WebClient client = new System.Net.WebClient();
       // parse the ftp host and file into a uri path for the upload
       Uri uri = new Uri(m_FtpHost + new FileInfo(dir).Name);
       // set the username and password for the FTP server
       client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
       // upload the file asynchronously, non-blocking.
       client.UploadFileAsync(uri, "STOR",dir);
       }
       catch(Exception e){

            print(e.Message);
       }
}

我可以找回上传的进度吗?我在 dirs 4-5 文件中。我想要确切的进度(不是上传的文件/(总文件))

编辑:因此正确的方法如下:

public int percentage;

try{
    // Get an instance of WebClient
    WebClient client = new System.Net.WebClient();
    // parse the ftp host and file into a uri path for the upload 
    Uri uri = new Uri(m_FtpHost + new FileInfo(dir).Name);
    // set the username and password for the FTP server                      
    client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
    // upload the file asynchronously, non-blocking.
    client.UploadProgressChanged += WebClientUploadProgressChanged; 
    client.UploadFileCompleted += WebClientUploadCompleted;
    client.UploadFileAsync(uri, "STOR",dir);

}

void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
    percentage =  e.ProgressPercentage;
}
void WebClientUploadCompleted(object sender, UploadFileCompletedEventArgs e)
{
    print( "Upload is finished. ");
}

我将此实现添加到我的代码中,但它似乎不会在控制台中打印任何内容。

【问题讨论】:

  • 看看这篇文章如何做到这一点:stackoverflow.com/questions/982299/…
  • @Sybren 您确定该解决方案吗?我将如何使用这种方法提供服务器的凭据?
  • @JoseRamon “我将如何提供服务器的凭据?”照原样,就像你现在做的一样......
  • 你可以用同样的方式提供凭证。

标签: c# upload ftp


【解决方案1】:

WebClient 包含一个专门的事件

 public event UploadProgressChangedEventHandler UploadProgressChanged 

https://msdn.microsoft.com/en-us/library/system.net.webclient.uploadprogresschanged(v=vs.110).aspx

编辑: HttpWebRequest 方法基于谷歌结果:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "text/plain";

        request.Timeout = -1; //Infinite wait for the response.

        // Get the file information object.
        FileInfo fileInfo = new FileInfo("C:\\Test\\uploadFile.dat");

        // Set the file content length.
        request.ContentLength = fileInfo.Length;
        // Get the number of segments the file will be uploaded to the stream.
        int segments = Convert.ToInt32(fileInfo.Length / (1024 * 4));

        // Get the source file stream.
        using (FileStream fileStream = fileInfo.OpenRead())
        {
            // Create 4KB buffer which is file page size.
            byte[] tempBuffer = new byte[1024 * 4];
            int bytesRead = 0;

            // Write the source data to the network stream.
            using (Stream requestStream = request.GetRequestStream())
            {
                // Loop till the file content is read completely.
                while ((bytesRead = fileStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0)
                {
                    // Write the 4 KB data in the buffer to the network stream.
                    requestStream.Write(tempBuffer, 0, bytesRead);

                    // Update your progress bar here using segment count.
                }
            }
        }
        // Post the request and Get the response from the server.
        using (WebResponse response = request.GetResponse())
        {
            // Request is successfully posted to the server.
        }

【讨论】:

  • 我按照这个例子进行了进度和完成。在 WebClientUploadCompleted 的情况下,我在上传完成时收到消息,但是在 progressChanged 的​​情况下,它没有在控制台中打印任何内容。我在这里缺少什么?
  • 你可以试试print(e.BytesSent * 100 / e.TotalBytesToSend);
  • 我尝试在 WebClientUploadProgressChanged 中添加 print("####") ,但似乎代码永远不会进入该函数。
  • 您应该首先添加 eventHandlers,然后调用将触发它们的方法。在client.UploadFileAsync(uri, "STOR",dir);之前这样做client.UploadProgressChanged += WebClientUploadProgressChanged;
  • 我试过了,我总是收到零进度。
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2011-02-19
  • 2018-03-25
  • 2011-11-14
  • 2016-11-26
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多