java - How to code MVC Web Api Post method for file upload -


i following this tutorial on uploading files server android, cannot seem code right on server side. can please me code web api post method work android java uploader? current web api controller class looks this:

using system; using system.collections.generic; using system.io; using system.linq; using system.net; using system.net.http; using system.threading.tasks; using system.web; using system.web.http;  namespace wsiswebservice.controllers {     public class filescontroller : apicontroller     {         // api/files         public ienumerable<string> get()         {             return new string[] { "value1", "value2" };         }          // api/files/5         public string get(int id)         {             return "value";         }          // post api/files         public string post([frombody]string value)         {             var task = this.request.content.readasstreamasync();             task.wait();             stream requeststream = task.result;              try             {                 stream filestream = file.create(httpcontext.current.server.mappath("~/" + value));                 requeststream.copyto(filestream);                 filestream.close();                 requeststream.close();             }             catch (ioexception)             {                 //  throw new httpresponseexception("a generic error occured. please try again later.", httpstatuscode.internalservererror);             }              httpresponsemessage response = new httpresponsemessage();             response.statuscode = httpstatuscode.created;             return response.tostring();         }                    // put api/files/5         public void put(int id, [frombody]string value)         {         }          // delete api/files/5         public void delete(int id)         {         }     } } 

i pretty desperate working deadline tuesday. if appreciated.

you can post files multipart/form-data

    // post api/files     public async task<httpresponsemessage> post()     {         // check if request contains multipart/form-data.         if (!request.content.ismimemultipartcontent())         {             throw new httpresponseexception(httpstatuscode.unsupportedmediatype);         }          string root = httpcontext.current.server.mappath("~/app_data");         var provider = new multipartformdatastreamprovider(root);          string value;          try         {             // read form data , return async data.             var result = await request.content.readasmultipartasync(provider);              // illustrates how form data.             foreach (var key in provider.formdata.allkeys)             {                 foreach (var val in provider.formdata.getvalues(key))                 {                     // return multiple value formdata                     if (key == "value")                         value = val;                 }             }                                     if (result.filedata.any())             {                                     // illustrates how file names uploaded files.                 foreach (var file in result.filedata)                 {                     fileinfo fileinfo = new fileinfo(file.localfilename);                     if (fileinfo.exists)                     {                        //do somthing file                     }                 }             }               httpresponsemessage response = request.createresponse(httpstatuscode.created, value);             response.headers.location = new uri(url.link("defaultapi", new { id = files.id }));             return response;         }         catch (system.exception e)         {             return request.createerrorresponse(httpstatuscode.internalservererror, e);         }     } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -