This article mainly introduces ASP.net webapi upload picture examples, the need for friends can refer to the
The code is as follows: [httppost] public task<hashtable> imgupload () { //check is Multipart/form-data if (! Request.Content.IsMimeMultipartContent ("Form-data")) throw new Httpresponseexception (Httpstatuscode.unsupportedmediatype); /File save directory path String Savetemppath = "~/sayplaces/" + "/saypic/saypictemp/"; String Dirtemppath = Httpcontext.curren T.server.mappath (Savetemppath); /Set upload directory var provider = new Multipartformdatastr Eamprovider (Dirtemppath); //var queryp = Request.getquerynamevaluepairs ();//Get the set of key values for the query string var task = Request.Content.ReadAsMultipartAsync (provider) . continuewith& Lt Hashtable> (o => { Hashtable hash = New Hashtable (); &NBSP; hash["error"] = 1; hash["errmsg" = "Upload error"; &NB Sp var file = provider. Filedata[0];//provider. formdata String orfilename = file. Headers.ContentDisposition.FileName.TrimStart (' "). TrimEnd (' "'); FileInfo FileInfo = new FileInfo (file. LocalFilename); //MAX File int maxSize = 10000000; &NBS P if (FileInfo. Length <= 0) { & nbsp hash["error"] = 1; hash["errmsg" = "Please select Upload file." "; } else if (FileInfo. Length > MaxSize) { &NB Sp hash["error"] = 1; hash["errmsg" = "Upload file size exceeds limit." "; } else &N Bsp { string fileext = Orfilename. Substring (Orfilename. LastIndexOf ('. ')); //definition allow uploaded file extensions &NB Sp String filetypes = "Gif,jpg,jpeg,png,bmp"; IF (String.IsNullOrEmpty (fileext) | | Array.indexof (Filetypes.split (', '), fileext.substring (1). ToLower ()) = = 1 &NBSP { hash["error" = 1;   ; hash["errmsg" = "Upload file name extension is not allowed extension." "; } Else { &NBS P String ymd = DateTime.Now.ToString ("YyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); & nbsp String NewFileName = DateTime.Now.ToString ("Yyyymmddhhmmss_ffff", System.Globalization.DateTimeFormatInfo.InvariantInfo); & nbsp FileInfo. CopyTo (Path.Combine (Dirtemppath, NewFileName + fileext), true); &NB Sp &nbSp FileInfo. Delete (); hash["error" = 0; &nbs P hash["errmsg"] = "Upload success"; } } R Eturn hash; }); return task; }