asp.net - Uploading a file to a webservice without saving to server, database, or locally -


i creating web service let user upload file , make changes file , return file. want file content without saving server, database, or locally. instead want save content stream or similar can run modifications on content , return new file.

i have tried doing following can figure out how upload , save locally. file information collected form data , file headers.

    namespace fileconverter.controllers {     [routeprefix("api/test")]     public class fileuploadcontroller : apicontroller     {         private static readonly string serveruploadfolder = "c:\\temp"; //path.gettemppath();          [route("files")]         [httppost]         [validatemimemultipartcontentfilter]         public async task<fileresult> uploadsinglefile()         {             var streamprovider = new multipartformdatastreamprovider(serveruploadfolder);             await request.content.readasmultipartasync(streamprovider);              return new fileresult             {                 filenames = streamprovider.filedata.select(entry => entry.localfilename),                 names = streamprovider.filedata.select(entry => entry.headers.contentdisposition.filename),                 contenttypes = streamprovider.filedata.select(entry => entry.headers.contenttype.mediatype),             };         }     } } 

i use validatemimemultipartcontentfilter attribute check if correct mime type sent.

namespace fileconverter.controllers {     public class validatemimemultipartcontentfilter : actionfilterattribute     {         public override void onactionexecuting(httpactioncontext actioncontext)         {             if (!actioncontext.request.content.ismimemultipartcontent())             {                 throw new httpresponseexception(httpstatuscode.unsupportedmediatype);             }         }          public override void onactionexecuted(httpactionexecutedcontext actionexecutedcontext)         {          }      } } 

i have model set store data.

namespace fileconverter.controllers {     public class fileresult     {         public ienumerable<string> filenames { get; set; }         public ienumerable<string> contenttypes { get; set; }         public ienumerable<string> names { get; set; }     } } 

is there way can content of file in stream/byte[] without saving file locally , reading content? if how go doing that?

by time form action invoked, asp.net has done lot of magic interpret file upload, can't within action itself. however, can capture byte streams in multipart forms come across wire using httpmodule or httphandler.

this example seems starting point.

i don't know whether httprequest can begin returning data until after payload has been received, going require crazy cross-request magic if literally want "stream" filtering process. on other hand, if intending keep whole file in memory while process , send afterward, i'd argue you're better off allowing mvc save file temp file: you'd potentially worse performance keeping entire uploaded file in memory while it's being uploaded.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -