///<summary>
/// 图片上传
///</summary>
///<returns></returns>
private string Upload_Images()
{
Boolean fileOK = false;
string imageFile = "";
String path = Server.MapPath("~/images/"); //设置服务器上传的路径,即文件上传的位置
if (fulImage.PostedFile.ContentLength / 1024 < 2000)
{
if (fulImage.HasFile)
{
//获取上传文件类型
String fileExtension = System.IO.Path.GetExtension(fulImage.FileName).ToLower();
//判断允许上传图片类型
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
break;
}
}
}
else if (!string.IsNullOrEmpty(this.imgPic.ImageUrl))
{
return imageFile = this.imgPic.ImageUrl;
}
if (fileOK)
{
try
{
//把当前时间取出,组成字符串,加入文件名称,防止重复命名
string fileName = DateTime.Now.ToString().Replace("-", "").Replace("", "").Replace(":", "") + fulImage.FileName;
fulImage.PostedFile.SaveAs(path + fileName);
imageFile = "../Images/" + fileName;
}
catch (Exception)
{
Response.Write("<script>alert('对不起,图片不能上传!')</script>");
}
}
else
{
if (string.IsNullOrEmpty(Request["newsId"]))//如果是新增显示提示,修改不显示提示
Response.Write("<script>alert('对不起,不能接受这种文件类型!')</script>");
}
}
else
{
Response.Write("<script>alert('对不起,图片过大不能上传!')</script>");
}
return imageFile;
}