Upload images through webapi,

Source: Internet
Author: User

Upload images through webapi,
Requirement: When uploading the student information, upload the profile picture information. The basic information table and the subject information table are one-to-many relationship tables (after adding the basic information, add the subject information ). Test:

[HttpPost] public string Post () {if (! Request. Content. IsMimeMultipartContent () throw new HttpResponseException (Request. CreateResponse (HttpStatusCode. NotAcceptable, "Invalid Request! "); // Get Student information Student model = new Student () {Name = HttpContext. current. request. form ["StuName"], GroupName = HttpContext. current. request. form ["GroupName"], //...}; // obtain the student's Subject name string passSubject = HttpContext. current. request. form ["passSubject"]; // obtain the student's failed account name string noPassSubject = HttpContext. current. request. form ["noPassSubject"]; Trace. writeLine ("begin add student information"); // Add student information stuService. addStuByAs Ync (model ). continueWith (p => {long stuId = p. result; Trace. writeLine ("begin through the subject"); subjectService. addPassSubject (passSubject, stuId); // Add this student Trace through the subject information. writeLine ("end through KE Jing"); Trace. writeLine ("begin failed"); subjectService. addPassSubject (noPassSubject, stuId); // Add Trace of the student's failed subject information. writeLine ("end failed to pass the Cortana") ;}); Trace. writeLine ("end add student information"); string path = System. web. httpContext. current. server. ma PPath ("~ /Images/upload/"); Trace. writeLine ("Get image ...... "); Request. content. readAsMultipartAsync (). continueWith (p => {var content = p. result. contents; Trace. writeLine ("begin image"); foreach (var item in content) {if (string. isNullOrEmpty (item. headers. contentDisposition. fileName) {continue;} item. readAsStreamAsync (). continueWith (a => {Stream stream =. result; string fileName = item. headers. contentDisposition. fileName; fileName = fileName. substring (1, fileName. length-2); Trace. writeLine ("image name:" + fileName); // convert stream to image saveImg (path, stream, fileName) ;});} Trace. writeLine ("end image") ;}); return "OK ";}

 

Form test:

<! DOCTYPE html> 

The result is as follows:

The output "image name" is displayed under "end image". The main thread continues to run down when the image is uploaded.

Similarly, when the AddStuByAsync () method is executed, the subscriber thread adds the subject information.

 

Related Article

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.