当前位置: 移动技术网 > IT编程>开发语言>.net > upload上传单张图片

upload上传单张图片

2017年12月12日  | 移动技术网IT编程  | 我要评论
通过upload上传单张图片,具体实现方式请看代码。  protected void btnpic_upload_click(object sender, e

通过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上传单张图片的全部代码,希望大家喜欢。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网