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.