requirements: Upload the student information at the same time upload the avatar information, the Student Basic information table and the chart of accounts for a one-to-many relationship table (add basic information after adding through the account information). Test:
[HttpPost] Public stringPost () {if(!Request.Content.IsMimeMultipartContent ())Throw NewHttpresponseexception (Request.createresponse (httpstatuscode.notacceptable,"Invalid request!")); //Get student InformationStudent model =NewStudent () {Name= httpcontext.current.request.form["Stuname"], GroupName= httpcontext.current.request.form["GroupName"], // ... }; //get learner by account name stringPasssubject = httpcontext.current.request.form["Passsubject"]; //get learner does not pass account name stringNopasssubject = httpcontext.current.request.form["NoPAsssubject"]; Trace.WriteLine ("begin to add student information"); //Add student InformationStuservice.addstubyasync (model). ContinueWith (p = { LongStuid =P.result; Trace.WriteLine ("begin by Chart of accounts"); Subjectservice.addpasssubject (Passsubject, stuid);//Add this learner through the account informationTrace.WriteLine ("end through the chart of accounts"); Trace.WriteLine ("begin does not pass the chart of accounts"); Subjectservice.addpasssubject (Nopasssubject, stuid);//Add this learner does not pass account informationTrace.WriteLine ("end does not pass the chart of accounts"); }); Trace.WriteLine ("End Add learner information"); stringPath = System.Web.HttpContext.Current.Server.MapPath ("~/images/upload/"); Trace.WriteLine ("Get Pictures ..."); Request.Content.ReadAsMultipartAsync (). ContinueWith (P= { varContent =p.result.contents; Trace.WriteLine ("begin picture"); foreach(varIteminchcontent) { if(string. IsNullOrEmpty (item. Headers.ContentDisposition.FileName)) {Continue; } item. Readasstreamasync (). ContinueWith (A={Stream Stream=A.result; stringFileName =item. Headers.ContentDisposition.FileName; FileName= Filename.substring (1, Filename.length-2); Trace.WriteLine ("Picture name:"+fileName); //Stream to Imagesaveimg (path, stream, fileName); }); } trace.writeline ("End Picture"); }); return "OK"; }
Form test:
<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title></title></Head><Body> <formenctype= "Multipart/form-data"Method= "POST"Action= "Http://localhost:17822/api/Student/Post"> <P><label>Student Name:</label> <inputname= "Stuname" /></P> <P><label>Group name</label><inputname= "GroupName" /></P> <P><label>By Account:</label> <inputname= "Passsubject" /></P> <P><label>Failed account:</label> <inputname= "Nopasssubject" /></P> <P><label>Avatar</label> <inputtype= "File"name= "Dfile" /></P> <P><label></label> <inputtype= "Submit" /></P> </form></Body></HTML>
Post-submission results are as follows:
by outputting "Picture name" under "End Picture", you can see that the main thread has continued to execute down while uploading.
The upper part is also the same, start the child thread to add the account information when the method Addstubyasync () finishes executing.
Webapi Uploading Images