您的位置:首页 > 其它

上传图片的简单方法

2006-07-31 11:26 309 查看
if (this.FileUpload1.PostedFile.FileName == string.Empty)
{
this.lblInfo.Text = "请选择要上传的图片";
}
else if (this.FileUpload1.PostedFile.ContentLength > 2 * 1024 * 1024)
{
this.lblInfo.Text = "图片不能大于2M";
}
else if (this.FileUpload1.PostedFile.ContentType.ToLower() != "image/pjpeg" && this.FileUpload1.PostedFile.ContentType.ToLower() != "image/gif")
{
this.lblInfo.Text = "系统仅支持JPG和GIF图片";
}

else
{
try
{
Random ran = new Random();
string random = ran.Next().ToString();
string time = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
string filename = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf("."));
string UploadFileDestination=HttpContext.Current.Request.PhysicalApplicationPath + "UPLOADFOLDER";
//判断文件夹是否存在,不存在则创建

this.FileUpload1.PostedFile.SaveAs(HttpContext.Current.Request.PhysicalApplicationPath + "UPLOADFOLDER//" + time + random + filename);
this.txtImgeURL.Text = HttpContext.Current.Request.ApplicationPath + "//UPLOADFOLDER//" + time + random + filename;
this.lblInfo.Text = "上传图片成功";
}
catch (Exception ex)
{
this.lblInfo.Text = "上传图片失败:"+ex.Message.ToString();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: