0

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?

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • Have you tried looking at `NameValueCollection`? Also, there is a [different question that might be of interest.](http://stackoverflow.com/questions/11048258/uploadfile-with-post-values-by-webclient) – techvice Nov 18 '16 at 22:32
  • I've seen this question and also a code [project article](http://www.codeproject.com/Articles/8600/UploadFileEx-C-s-WebClient-UploadFile-with-more-fu), but I'm thinking there must be a way to change the name of the input field using a supported way of `WebClient`. Microsoft wouldn't just "assume" every file must be in a control called exactly **"file"**. – bytecode77 Nov 18 '16 at 22:34

1 Answers1

3

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.

techvice
  • 1,315
  • 1
  • 12
  • 24
  • So it looks like we really can't change the name here. Hopefully, Microsoft will make it available as a parameter some day... – bytecode77 Nov 18 '16 at 22:53
  • Realized this when calling the webapi from the VS-MVC-angular based frontend is calling api via webclient.UploadFile - the API needs to have same name as file - if you are using (the recommended) - IFormFile file in .net core 2 – hB0 Nov 14 '18 at 11:23