Webapi Uploading Images

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.