c# - Is it possible to override MultipartFormDataStreamProvider so that is doesn't save uploads to the file system? -
i have asp.net web api application allows clients (html pages , iphone apps) upload images to. using async upload task described in article.
everything works great when want save file system because that's code automatically, behind scenes seems. but, don't want save uploaded files file system. instead, want take uploaded stream , pass through amazon s3 bucket using aws sdk .net.
i have code set send stream aws. problem can't figure out how uploaded content stream web api method instead of having automatically save disk.
i hoping there virtual method override in multipartformdatastreamprovider allow me else uploaded content other save disk, there doesn't seem be.
any suggestions?
you override multipartformdatastreamprovider's getstream method return stream not file stream aws stream, there issues doing so(which not elaborate here). instead create provider deriving abstract base class multipartstreamprovider. following sample heavily based on actual source code of multipartformdatastreamprovider , multipartfilestreamprovider. can check here , here more details. sample below:
public class custommultipartformdatastreamprovider : multipartstreamprovider { private namevaluecollection _formdata = new namevaluecollection(stringcomparer.ordinalignorecase); private collection<bool> _isformdata = new collection<bool>(); private collection<mymultipartfiledata> _filedata = new collection<mymultipartfiledata>(); public namevaluecollection formdata { { return _formdata; } } public collection<multipartfiledata> filedata { { return _filedata; } } public override stream getstream(httpcontent parent, httpcontentheaders headers) { // form data, content-disposition header requirement contentdispositionheadervalue contentdisposition = headers.contentdisposition; if (contentdisposition != null) { // if have file name write contents out aws stream. otherwise write memorystream if (!string.isnullorempty(contentdisposition.filename)) { // won't post process files form data _isformdata.add(false); mymultipartfiledata filedata = new mymultipartfiledata(headers, your-aws-filelocation-url-maybe); _filedata.add(filedata); return myawsstream;//**return aws stream here** } // post process form data _isformdata.add(true); // if no filename parameter found in content-disposition header return memory stream. return new memorystream(); } throw new invalidoperationexception("did not find required 'content-disposition' header field in mime multipart body part.."); } /// <summary> /// read non-file contents form data. /// </summary> /// <returns></returns> public override async task executepostprocessingasync() { // find instances of httpcontent created memory stream , read them asynchronously // string content , add form data (int index = 0; index < contents.count; index++) { if (_isformdata[index]) { httpcontent formcontent = contents[index]; // extract name content-disposition header. know earlier header present. contentdispositionheadervalue contentdisposition = formcontent.headers.contentdisposition; string formfieldname = unquotetoken(contentdisposition.name) ?? string.empty; // read contents string data , add form data string formfieldvalue = await formcontent.readasstringasync(); formdata.add(formfieldname, formfieldvalue); } } } /// <summary> /// remove bounding quotes on token if present /// </summary> /// <param name="token">token unquote.</param> /// <returns>unquoted token.</returns> private static string unquotetoken(string token) { if (string.isnullorwhitespace(token)) { return token; } if (token.startswith("\"", stringcomparison.ordinal) && token.endswith("\"", stringcomparison.ordinal) && token.length > 1) { return token.substring(1, token.length - 2); } return token; } } public class mymultipartfiledata { public multipartfiledata(httpcontentheaders headers, string awsfileurl) { headers = headers; awsfileurl = awsfileurl; } public httpcontentheaders headers { get; private set; } public string awsfileurl { get; private set; } }
Comments
Post a Comment