asp.net webapi Upload Picture examples _ practical skills

Source: Internet
Author: User

Copy Code code as follows:

[HttpPost]
Public task{
Check to see if it's 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.Current.Server.MapPath (Savetemppath);
Set up upload Directory
var Provider = new Multipartformdatastreamprovider (Dirtemppath);
var queryp = Request.getquerynamevaluepairs ();//Get the collection of key values for the query string
var task = Request.Content.ReadAsMultipartAsync (provider).
Continuewith
{
Hashtable hash = new Hashtable ();
hash["Error"] = 1;
hash["errmsg"] = "Upload error";
var file = provider. Filedata[0];//provider. FormData
String orfilename = file. Headers.ContentDisposition.FileName.TrimStart (' "). TrimEnd (' ");
FileInfo FileInfo = new FileInfo (file. LocalFilename);
Maximum file size
int maxSize = 10000000;
if (FileInfo. Length <= 0)
{
hash["Error"] = 1;
hash["errmsg"] = "Please select Upload file." ";
}
else if (FileInfo. Length > MaxSize)
{
hash["Error"] = 1;
hash["errmsg" = "Upload file size exceeds limit." ";
}
Else
{
String fileext = Orfilename. Substring (Orfilename. LastIndexOf ('. '));
Define file extensions to allow uploads
String filetypes = "Gif,jpg,jpeg,png,bmp";
if (String.IsNullOrEmpty (fileext) | | Array.indexof (Filetypes.split (', '), fileext.substring (1). ToLower ()) = =-1)
{
hash["Error"] = 1;
hash["errmsg" = "Upload file name extension is not allowed." ";
}
Else
{
String ymd = DateTime.Now.ToString ("YyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
String NewFileName = DateTime.Now.ToString ("Yyyymmddhhmmss_ffff", System.Globalization.DateTimeFormatInfo.InvariantInfo);
FileInfo. CopyTo (Path.Combine (Dirtemppath, NewFileName + fileext), true);
FileInfo. Delete ();
hash["error"] = 0;
hash["errmsg"] = "upload success";
}
}
return hash;
});
return task;
}

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.