asp.net webapi upload Picture example

Source: Internet
Author: User
Tags datetime file size hash

  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 ();              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;    &nbsp ;                 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; }   
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.