我不断收到“只允许图像”,我尝试了“file.PostedFile.FileName”也不起作用!
这段代码写在一个单独的类中..
public static String UploadFile(FileUpload file, String type, out String filename)
{
String ext = System.IO.Path.GetExtension(file.FileName);
filename = "";
if (file.PostedFile.ContentLength > 2000000)
{
return "File is larger than 2 MB";
}
else if (type != "File")
{
if (ext.ToLower() != ".jpg" || ext.ToLower() != ".png" || ext.ToLower() != ".gif" || ext.ToLower() != ".jpeg")
{
return "Only images are allowed";
}
else
{
filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
String root = HttpContext.Current.Server.MapPath("~/Images/");
file.SaveAs(root + type + "/" + filename);
return "Success";
}
}
else
{
filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
String root = HttpContext.Current.Server.MapPath("~/Files/");
file.SaveAs(root + filename);
return "Success";
}
}