bytecode77 bytecode77 - 21 days ago 5
C# Question

How can I specify form name in WebClient.UploadFile?

WebClient
is a very decent layer of abstraction when compared to
HttpPostRequets
, in most situations.

using (WebClient webClient = new WebClient())
{
return webClient.UploadFile(url, path);
}


However, the class doesn't seem to include an option for the name of the input field. Here,
"file"
must be the name of the input field.

From Wireshark


Content-Disposition: form-data; name="file"; filename="test.txt"


Is there a way to specify the "name" field, without going back to HttpPostRequest?

Answer

It would appear that this is hardcoded. View source here for WebClient, head to line 573.

Looks like you will have to implement that portion yourself if you would like to do that. I found blog post that looked promising (since it had the name customized). You can find that here.