[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;
}