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?

2
  • Have you tried looking at NameValueCollection? Also, there is a different question that might be of interest. Commented Nov 18, 2016 at 22:32
  • I've seen this question and also a code project article, 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". Commented Nov 18, 2016 at 22:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So it looks like we really can't change the name here. Hopefully, Microsoft will make it available as a parameter some day...
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.