c# - How to post MultipartFormDataContent (with attached files) to REST API? -
i have api in asp.net mvc:
public class uploadcontroller : controller { ... public actionresult post(uploadmodel uploadmodel)
i wish call api test client, attaching files.
using (var httpclient = new httpclient()) { httpclient.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("multipart/form-data")); var bytearraycontent = new bytearraycontent(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); bytearraycontent.headers.contenttype = mediatypeheadervalue.parse("text/csv"); var uploadmodel = new uploadmodel { allergene = "anders", description = "desc", email = "and@juu.com" }; var content = new stringcontent(jsonconvert.serializeobject(uploadmodel), encoding.utf8, "application/json"); var multipartformdatacontent = new multipartformdatacontent { {content}, {bytearraycontent, "\"file\"", "\"feedback.csv\""} }; var result = httpclient.postasync("http://localhost:53411/upload/post", multipartformdatacontent).result; }
if post multipartformdatacontent don't attributes on model. if post content, attributes, not attached file.
what missing?
Comments
Post a Comment