Does anyone know how to use the HttpClient
in .Net 4.5 with multipart/form-data
upload?
I couldn't find any examples on the internet.
Does anyone know how to use the HttpClient
in .Net 4.5 with multipart/form-data
upload?
I couldn't find any examples on the internet.
Here is another example on how to use
HttpClient
to upload amultipart/form-data
.It uploads a file to a REST API and includes the file itself (e.g. a JPG) and additional API parameters. The file is directly uploaded from local disk via
FileStream
.See here for the full example including additional API specific logic.
It works more or less like this (example using an image/jpg file):
(You can
requestContent.Add()
whatever you want, take a look at the HttpContent descendant to see available types to pass in)When completed, you'll find the response content inside
HttpResponseMessage.Content
that you can consume withHttpContent.ReadAs*Async
.Try this its working for me.
my result looks like this:
This is an example of how to post string and file stream with HTTPClient using MultipartFormDataContent. The Content-Disposition and Content-Type need to be specified for each HTTPContent:
Here's my example. Hope it helps:
Here's a complete sample that worked for me. The
boundary
value in the request is added automatically by .NET.