使用WebClient上传文件目录 [英] Upload directory of files using WebClient
问题描述
我已经搜索和搜索,找不到一种方法来做到这一点。我在我想上传的目录中有文件。文件名称不断变化,所以我无法通过文件名进行上传。这是我尝试过的。使用(WebClient客户端=新的WebClient())
{
client.Credentials = new NetworkCredential() 用户密码);
foreach(文件中的var filePath)
client.UploadFile(ftp://site.net//PICS_CAM1//,STOR,@PICS_CAM1\);
}
但是我收到一个编译错误:
名称'files'在当前上下文中不存在
我研究说,这应该工作。
有没有人有通过 WebClient
上传文件目录的好方法?
您必须定义并设置文件
。如果您想上传某个本地目录中的所有文件,请使用 Directory.EnumerateFiles
。
地址
WebClient.UploadFile
必须是目标文件的完整URL,而不仅仅是目标目录的URL。
的IEnumerable<串GT; files = Directory.EnumerateFiles(@C:\local\folder); (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(username,password);
foreach(文件中的字符串文件)
{
client.UploadFile(
ftp://example.com/remote/folder/+ Path.GetFileName (文件),文件);
}
}
I have searched and searched and cannot find a way to do this. I have files in a directory I want to upload. The file names change constantly so I cannot upload by file name. Here is what I have tried.
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("User", "Password");
foreach (var filePath in files)
client.UploadFile("ftp://site.net//PICS_CAM1//", "STOR", @"PICS_CAM1\");
}
But I am getting a compiler error:
The name 'files' does not exist in the current context
Everything I have researched says this should work.
Does anyone have a good way to upload a directory of files via WebClient
?
You have to define and set the files
. If you wanted to upload all files in a certain local directory, use for example Directory.EnumerateFiles
.
Also the address
argument of WebClient.UploadFile
has to be a full URL to a target file, not just a URL to a target directory.
IEnumerable<string> files = Directory.EnumerateFiles(@"C:\local\folder");
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
foreach (string file in files)
{
client.UploadFile(
"ftp://example.com/remote/folder/" + Path.GetFileName(file), file);
}
}
这篇关于使用WebClient上传文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!