复制代码 代码如下:
[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))//不存在这个文件夹就创建这个文件夹
{
Directory.CreateDirectory(Server.MapPath("~/UploadImages/"));
}
uploadFile.SaveAs(Server.MapPath("~/UploadImages/") + uploadFileName);
message = uploadFileName;
return message;
}
catch (Exception ex)
{
message = ex.Message;
return message;
}
}
}
}
您可能感兴趣的文章:- C#图片切割、图片压缩、缩略图生成代码汇总
- c# Base64编码和图片的互相转换代码
- C#图片压缩的实现方法
- C#实现图片分割方法与代码
- C#识别出图片里的数字和字母
- C#图片按比例缩放的实现代码
- c#给图片添加文字的代码小结
- C#(.net)水印图片的生成完整实例
- asp.C#实现图片文件与base64string编码解码
- c#图片缩放图片剪切功能实现(等比缩放)
- C#实现图片切割的方法