限制用户上传图片大小

protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
            {
                System.Drawing.Image img = System.Drawing.Image.FromStream(filePhoto.PostedFile.InputStream);
                int Width = img.Width;
                int Height = img.Height;
                if (Width > 250 || Height > 350 || filePhoto.PostedFile.ContentLength > 1024 * 1024 * 2)
                {
                    Alert.Show("不符合上传要求");
                    //Response.Write("不符:Width=" + Width.ToString() + "<br>Height=" + Height.ToString() + "<br>Size=" + (this.filePhoto.PostedFile.ContentLength / 1024).ToString("##,##0.00") + "K");
                }
                else
                {
                        string fileName = txtNo.Text.Trim() + ".jpg";
                        filePhoto.SaveAs(Server.MapPath("~/Resources/upload/" + fileName));
                        imgPhoto.ImageUrl = "~/Resources/upload/" + fileName;
                        // 清空文件上传组件
                        filePhoto.Reset();
                }
            }
            else
            {
                Alert.Show("请选择图片文件!");
            }
        }

 

posted @ 2014-04-19 15:31  胡狗880601  阅读(426)  评论(0编辑  收藏  举报