上传图片和图片处理

 function CheckFile(str)
   {
        var strRegex = "(.jpg|.JPG|.gif|.GIF)$"; //用于验证图片扩展名的正则表达式
        var re=new RegExp(strRegex);

        if (re.test(str)){
            document .getElementById ("Photo").style .display ="none";
            document .getElementById ("newPreview").style .display ="block";
            PreviewImg(str);
            return (true);
        }
        else{
            document .getElementById ("Photo").style .display ="block";
            document .getElementById ("newPreview").style .display ="none";
            document .getElementById ("PhotoUpload").value="";
            alert("禁止的文件扩展名");
            return (false);
        }
   }
   function PreviewImg(imgFile)

    var newPreview = document.getElementById("newPreview");
    newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile;
    newPreview.style.width = "502px";
    newPreview.style.height = "375px";
}

 

.cs

protected void PhotoUp()
    {
        string Oldfilename = PhotoUpload.PostedFile.FileName;
        if (Oldfilename != null && Oldfilename != "" && Oldfilename.Length > 1)
        {
            if (PhotoUpload.PostedFile.ContentLength <= 2097152)
            {
                try
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(PhotoUpload.PostedFile.InputStream);
                    try
                    {
                        Oldfilename = System.IO.Path.GetFileName(Oldfilename);

                        int index = Oldfilename.LastIndexOf(".");
                        string lastName = Oldfilename.Substring(index, Oldfilename.Length - index);
                        if (lastName == ".JPG" || lastName == ".GIF" || lastName == ".jpg" || lastName == ".gif")
                        {
                            string filename = string.Empty;
                            if (HFPhotoFiles.Value.Trim() != "" && HFPhotoFiles.Value.Trim() != null)
                            {
                                filename = HFPhotoFiles.Value.Trim();
                            }
                            else
                            {
                                filename = Convert.ToString(Guid.NewGuid()) + lastName;
                            }
                            PhotoUpload.PostedFile.SaveAs(Server.MapPath("~/GroupImages/" + filename));
                            HFPhotoFiles.Value = filename;
                        }
                    }
                    catch
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('照片上传失败!')</script>");
                    }
                }
                catch
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('您上传的不是图片文件!其他信息保存成功!')</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('上传照片太大,照片应小于等于2M!')</script>");
            }
        }
    }

 

.aspx

 <tr>
                                                            <td height="20px" align="right">
                                                                医院照片:</td>
                                                                <td align="left" colspan =3>
                                                                    &nbsp;<asp:FileUpload ID="PhotoUpload" runat="server" CssClass="textfield" οnchange="CheckFile(this.value);" Height="20px" Width="219px" /></td>
                                                        </tr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值