[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#保存上传来的图片示例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)