[英]C# Change content-disposition with webclient
我想使用C#和WebClient类在特定网站上上传文件。 我有这个代码:
Console.Write("\nPlease enter the URI to post data to : ");
String uriString = "http://www.noelshack.com/api.php";
// String uriString = "http://127.0.0.1/upload.php";
WebClient myWebClient = new WebClient();
string fileName = lst_path[0];
byte[] responseArray = myWebClient.UploadFile(uriString,"POST", fileName);
MessageBox.Show("\nretour:" + System.Text.Encoding.ASCII.GetString(responseArray));
但是问题是,在网站上,文件输入的名称为“ fichier”,而webclient发送“ file”作为名称。
我希望它发送:
内容处置:表单数据; name =“ fichier”; filename =“ csharp.jpg”代替:Content-Disposition:form-data; name =“文件”; filename =“ csharp.jpg”
我没有找到如何修改此字段的方法,请提供一些帮助?
如果您拥有.NET 4.5以上版本,则可以使用HttpClient
类进行异步发布:
async Task<string> UploadFileAsync(string[] lst_path)
{
string uriString = "http://www.noelshack.com/api.php";
string fileName = lst_path[0];
using (HttpClient client = new HttpClient())
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (MultipartFormDataContent form = new MultipartFormDataContent())
using (StreamContent sc = new StreamContent(fs))
{
form.Add(sc, "fichier", fileName);
HttpResponseMessage response = await client.PostAsync(uriString, form);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.