c# - Send raw JSON and file in the same web api 2 endpoint -


i wondering whether possible send file (which want ".pdf", ".jpg" or ".png") along raw json. endpoints far send raw json (which i'm testing via postman frontend not exist yet), intention sending of form data sent using angular js. don't know angular js yet, can't imagine how work.

the signature of endpoint in question looks this:

[route("post")]     [customauthorize(roles = "user, admin")]     [validatejwt]     public async task<ihttpactionresult> post(httprequestmessage request, salesorderviewmodel orderdata) 

the view model c# class loads of string properties model binder converts json.

i know whether sending raw json , file user select possible in same endpoint web api 2. it?

thanks in advance.

you can't direct post aplication/json, still can multiple form fields (as form data), file + data, value of data can json.

i'm not recommending method trick:

public async task<ihttpactionresult> post()         {             if (!request.content.ismimemultipartcontent())             {                 request.createresponse(httpstatuscode.unsupportedmediatype);             }              //load in memory stream or in azure blob storage             var uploadfolder = "~/app_data/fileuploads"; // demonstrate upload please don't comment i'm saving file, don't recommend under no circumstance             var root = httpcontext.current.server.mappath(uploadfolder);             directory.createdirectory(root);             var provider =  new multipartformdatastreamprovider(root);             var result = await request.content.readasmultipartasync(provider);              if (result.filedata.firstordefault() == null)             {                 return badrequest("no import file attached");             }              var uploadedfileinfo = new fileinfo(result.filedata.first().localfilename);              var model = result.formdata["model"];              if (model == null)             {                 return badrequest("model missing");             }              var parameters = jsonconvert.deserializeobject<coords>(model);              var bytearray = file.readallbytes(uploadedfileinfo.fullname);            //..process bytes            //..process json passed in headers } 

and model:

public class coords     {         public cord[] cords { get; set; }     }      public class cord     {         public int x { get; set; }         public object y { get; set; }     } 

postman call:

enter image description here


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -