复制代码
- protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- string serverUpload = Server.MapPath("tupian/UpLoad");
- if (!Directory.Exists(serverUpload))
- {
- Directory.CreateDirectory(serverUpload);
- }
- string ImgPath = FileUpload1.PostedFile.FileName;//文件全路径
- string imgType = FileUpload1.PostedFile.ContentType;//获取上传文件的类型
- int ImgLength = FileUpload1.PostedFile.ContentLength;//获取上传文件的大小(单位字节)
- string newImageName = "XUHAI_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour +
- DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + Path.GetExtension(ImgPath);
- string serverImgName = serverUpload + "/" + newImageName;
- if (imgType.Substring(0, 5) == "image")
- {
- if (ImgLength <= 3048576)//如果文件大小于3M允许上传
- {
- FileUpload1.PostedFile.SaveAs(serverImgName);
- if (imgType.Substring(imgType.LastIndexOf("/") + 1) == "gif")
- {
- System.Drawing.Image img = System.Drawing.Image.FromFile(serverImgName);
- System.Drawing.Imaging.FrameDimension imgfrm = new FrameDimension(img.FrameDimensionsList[0]);
- int nframe = img.GetFrameCount(imgfrm);
- if (nframe > 1)
- {
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('只允许上传BMP|JPEG|PNG格式的图片
- ,不允许上传动画');</script>");
- oImage.Src = "";
- oImage.Width = 0;
- oImage.Height = 0;
- return;
- }
- }
- oImage.Height = 330;
- oImage.Width = 520;
- oImage.Src = "tupian/UpLoad/" + newImageName;
- HyperLink1.NavigateUrl = "tupian/UpLoad/" + newImageName;
- FileInfo fi = new FileInfo(Server.MapPath("tupian/UpLoad/" + newImageName));
- HyperLink1.Text = newImageName + "[上传时间:" + fi.CreationTime + "]";
- }
- else
- {
- oImage.Src = "";
- oImage.Width = 0;
- oImage.Height = 0;
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传图片不能超过3M');</script>");
- }
- }
- else
- {
- oImage.Src = "";
- oImage.Width = 0;
- oImage.Height = 0;
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('图片格式不正确');</script>");
- }
- }
- else
- {
- oImage.Src = "";
- oImage.Width = 0;
- oImage.Height = 0;
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择需要上传的图片');</script>");
- }
- }
1.87 MB, 下载次数: 156
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |