簡體   English   中英

C#使用WebClient更改內容處置

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM