欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#保存上傳來的圖片示例代碼

 更新時(shí)間:2013年11月08日 17:02:14   作者:  
保存上傳圖片的方法有很多,在接下來的文章中為大家詳細(xì)介紹下使用C#是如何做到的,感興趣的朋友不要錯(cuò)過
復(fù)制代碼 代碼如下:

[HttpPost]
public string UploadImage()
{
//string ss = Request.Form["uploadFile"];
//return ss;
HttpPostedFileBase uploadFile = Request.Files[0];
string fileName = uploadFile.FileName;
int fileSize = uploadFile.ContentLength;
string fileExt = Path.GetExtension(fileName).ToLower();
string message = "";
if (!(fileExt == ".png" || fileExt == ".gif" || fileExt == ".jpg" || fileExt == ".jpeg"))
{
message = "圖片類型只能為gif,png,jpg,jpeg";
return message;
}
else
{
if (fileSize > (int)(500 * 1024))
{
message = "圖片大小不能超過500KB";
return message;
}
else
{
Random r = new Random();
string uploadFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + r.Next(100000, 999999) + fileExt;
try
{
string directoryPath = Server.MapPath("~/UploadImages/");
if (!Directory.Exists(directoryPath))//不存在這個(gè)文件夾就創(chuàng)建這個(gè)文件夾
{
Directory.CreateDirectory(Server.MapPath("~/UploadImages/"));
}
uploadFile.SaveAs(Server.MapPath("~/UploadImages/") + uploadFileName);
message = uploadFileName;
return message;
}
catch (Exception ex)
{
message = ex.Message;
return message;
}
}
}
}

相關(guān)文章

最新評(píng)論