【发布时间】: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 “我将如何提供服务器的凭据?”照原样,就像你现在做的一样......
-
你可以用同样的方式提供凭证。