html - MultipartMemoryStreamProvider and reading user data from MultiPart/Form Data -
i have file , user data being posted multipart/form data post method in apicontroller class. able read file without problems unable read user data.
i tried couple of things using model binding, passing individual fields method parameter in post method get: no mediatypeformatter available read object of type 'formdatacollection' content media type 'multipart/form-data'.
var provider = await request.content.readasmultipartasync(new multipartmemorystreamprovider()); foreach (var item in provider.contents) { var fieldname = item.headers.contentdisposition.name.trim('"'); if (item.headers.contentdisposition.filename == null) { var data = await item.readasstringasync(); if(fieldname = 'name') name=data; } else filecontents = await item.readasbytearrayasync(); }
thanks.
i had considered adding other post per comment, (as decided), separate question.
public async task<httpresponsemessage> post() { if (!request.content.ismimemultipartcontent()) throw new httpresponseexception(httpstatuscode.unsupportedmediatype); try { string root = httpcontext.current.server.mappath("~/app_data"); var provider = await request.content.readasmultipartasync(new multipartformdatastreamprovider(root)); // file data foreach (multipartfiledata file in provider.filedata) { using (var ms = new memorystream()) { var diskfile = new filestream(file.localfilename, filemode.open); await diskfile.copytoasync(ms); var bytearray = ms.toarray(); } } // form data foreach (var key in provider.formdata.allkeys) { var values = provider.formdata.getvalues(key); if (values != null) { foreach (var value in values) { console.writeline(value); } } } return request.createresponse(httpstatuscode.created); } catch (exception ex) { return request.createerrorresponse(httpstatuscode.internalservererror, ex); } }
Comments
Post a Comment