通过http post将文件上传到服务器的最有效方法是什么?
我正在使用WebClient和线程:
public static void Uploadfile(string file_path, string SERVER_URL)
{
string filename = file_path;//Asign a value anyway in case of error.
try
{
filename = Path.GetFileName(file_path);
//Parameter for pass the name of the file
NameValueCollection parameters= new NameValueCollection();
parameters.Add("file", filename);
WebClient client = new WebClient();
//PUT Request
client.QueryString = parameters;
byte[] rawResponse = client.UploadFile(SERVER_URL, "POST", file_path);
Console.WriteLine(filename + " Uploaded!");
client.Dispose();
}
catch (Exception err)
{
Console.WriteLine(filename + " NOT uploaded: " + err.Message);
}
}
我在线程中这样调用函数:
for(xxx)
tasks.Add(Task.Run(() => Uploadfile(file_path, SERVER_URL)));
我为每个文件都打开一个实例,是不是很糟糕?WebClient是最有效的?
你有什么建议吗?