HttpClient Multipart Form Post in C#

前端 未结 4 2128
既然无缘
既然无缘 2020-12-12 22:24

I\'m trying to do a multipart form post using the HttpClient in C# and am finding the following code does not work.

Important:

var j         


        
4条回答
  •  既然无缘
    2020-12-12 22:38

    So the problem I'm seeing is that the MultipartFormDataContent request message will always set the content type of the request to "multipart/form-data". Endcoding json and placing that into the request only "looks" like to the model binder as a string.

    Your options are:

    • have your mvc action method receive a string and deserialize into your object
    • post each property of your model as a form part
    • create a custom model binder that will handle your request.
    • Breakup the operation into two posts, first sends the json metadata, the other sends the file. The response from the server should send some id or key to correlate the two requests.

    Reading through the RFC document and the MSDN documentation you may be able to do this, if you replace MultipartFormDataContent with MultipartContent. But I have not tested this yet.

提交回复
热议问题