如何在使用.net WebClient FileUpload时修改Content-Disposition Header

时间:2011-02-23 15:33:24

标签: .net file-upload upload header webclient

使用.net WebClient UploadFile时,是否可以在multipart标头中手动设置name属性?或者还有另一种解决方法吗?

-----------------------8cda1896efcd67f
Content-Disposition: form-data; **name="file"**; filename="Testfile.txt"
Content-Type: application/octet-stream

This is a Testfile.
-----------------------8cda1896efcd67f--

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式使用MultipartFormDataContent:

MultipartFormDataContent form = new MultipartFormDataContent();
string filename = @"Dir:\Testfile.txt"       
    , name = "file";
byte[] bytes = System.IO.File.ReadAllBytes(filename);

form.Add(new StringContent(name), "file");
form.Add(new ByteArrayContent(bytes), "File", Path.GetFileName(filename));