星空网 > 软件开发 > ASP.net

Upload图片

上传图片全不怕,轻松实现图片上传, 可以实现显示缩略图喔: 

后台代码: 
    protected void btnpic_upload_Click(object sender, EventArgs e) { #region 上传文件 Boolean fileOk = false; if (pic_upload.HasFile)//验证是否包含文件 { //取得文件的扩展名,并转换成小写 string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower(); //验证上传文件是否图片格式 fileOk = IsImage(fileExtension); if (fileOk) { //对上传文件的大小进行检测,限定文件最大不超过8M if (pic_upload.PostedFile.ContentLength < 8192000) { string filepath = "~/Admin/I_Institution/Images/"; if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(Server.MapPath(filepath)); } string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//这是存到服务器上的虚拟路径 string mappath = Server.MapPath(virpath);//转换成服务器上的物理路径 pic.Visible = true; pic_upload.PostedFile.SaveAs(mappath);//保存图片 //显示图片 pic.ImageUrl = virpath; lbl_pic.Visible = true; //清空提示 lbl_pic.Text = "上传成功"; } else { pic.Visible = false; lbl_pic.Visible = true; pic.ImageUrl = ""; lbl_pic.Text = "文件大小超出8M!请重新选择!"; } } else { lbl_pic.Visible = false; pic.ImageUrl = ""; lbl_pic.Text = "要上传的文件类型不对!请重新选择!"; } } else { lbl_pic.Visible = false; pic.ImageUrl = ""; lbl_pic.Text = "请选择要上传的图片!"; } #endregion }

 

       

    /// <summary>    /// 验证是否指定的图片格式    /// </summary>    /// <param name="str"></param>    /// <returns></returns>    public bool IsImage(string str)    {      bool isimage = false;      string thestr = str.ToLower();      //限定只能上传jpg和gif图片      string[] allowExtension = { ".jpg", ".gif", ".bmp", ".png" };      //对上传的文件的类型进行一个个匹对      for (int i = 0; i < allowExtension.Length; i++)      {        if (thestr == allowExtension[i])        {          isimage = true;          break;        }      }      return isimage;    }    /// <summary>    /// 创建一个指定长度的随机salt值    /// </summary>    public string CreateSalt(int saltLenght)    {      //生成一个加密的随机数      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();      byte[] buff = new byte[saltLenght];      rng.GetBytes(buff);      //返回一个Base64随机数的字符串      return Convert.ToBase64String(buff);    }    /// <summary>    /// 返回加密后的字符串    /// </summary>    public string CreatePasswordHash(string pwd, int saltLenght)    {      string strSalt = CreateSalt(saltLenght);      //把密码和Salt连起来      string saltAndPwd = String.Concat(pwd, strSalt);      //对密码进行哈希      string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");      //转为小写字符并截取前16个字符串      hashenPwd = hashenPwd.ToLower().Substring(0, 16);      //返回哈希后的值      return hashenPwd;    }

    

 拿到上传后的图片路径:    
      string IconUrl = this.pic.ImageUrl.Trim();
       model.IconUrl = Path.GetFileName(IconUrl); //获得已上传 图片控件的URL

   前台代码:

 <tr>            <td height="25" width="30%" align="right">              机构图标路径 :            </td>            <td height="25" width="*" align="left">              <asp:Image ID="pic" runat="server" Width="200px" Visible="False" /><br />              <asp:FileUpload ID="pic_upload" runat="server" />              <asp:Button ID="btnpic_upload" runat="server" Text="图片开始上传" OnClick="btnpic_upload_Click" /><br />              <asp:Label ID="lbl_pic" runat="server" Text="" Visible="False"></asp:Label>            </td>          </tr>

 




原标题:Upload图片

关键词:图片

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

VAT注册的步骤及服务流程详解:https://www.kjdsnews.com/a/1338980.html
VAT注册的步骤及代理服务介绍:https://www.kjdsnews.com/a/1338981.html
VAT注册的全流程指南:https://www.kjdsnews.com/a/1338982.html
VAT注册申报流程详解:https://www.kjdsnews.com/a/1338983.html
VAT注册申报流程指南:https://www.kjdsnews.com/a/1338984.html
VAT注销后,店铺会受到什么影响?:https://www.kjdsnews.com/a/1338985.html
深圳西乡三月三北帝庙会的千岁盆菜多少钱:https://www.vstour.cn/a/365180.html
TikTok SEO:TikTok会取代谷歌吗?:https://www.kjdsnews.com/a/1836538.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流