Cyb
Cyb

Reputation: 41

HttpClient post form with optional upload files

I have a web form with several options for uploading photos (up to 10). ContentType set to "multipart/form-data". I know how to upload just one file, but have no idea how to create HttpContent for other 9 optional? Creating MultipartContent with appropriate ContentDisposition header gave no luck. At very best I got:

Content-Disposition: form-data; name="bla bla bla";

Or, if I add parameters to ContentDisposition header:

Content-Disposition: form-data; name="bla bla bla"; filename;

I need to send post request with empty content for optional upload files like this:

Content-Disposition: form-data; name="bla bla bla"; filename=""

Any ideas appreciated.

Upvotes: 0

Views: 784

Answers (1)

Cyb
Cyb

Reputation: 41

OK, I don't know if it is a best solution for my question, but at least it works:
1. Create new class for HttpContent (subclass from HttpContent).
2. Initialize Content-Disposition header.
3. Override two methods TryComputeLength(return false) and SerializeToStreamAsync(just a stub for serialization task).

Now this class can be added to MultipartFormDataContent.
And http request content looks like:

----------[boundary string]
Content-Disposition: form-data; name="formFieldName"; filename=""

----------[boundary string]

Upvotes: 1

Related Questions