当前位置:Gxlcms > asp.net > upload上传单张图片

upload上传单张图片

时间:2021-07-01 10:21:17 帮助过:44人阅读

通过Upload上传单张图片,具体实现方式请看代码。 

  1. protected void btnpic_upload_Click(object sender, EventArgs e)
  2. {
  3. #region 上传文件
  4. Boolean fileOk = false;
  5. if (pic_upload.HasFile)//验证是否包含文件
  6. {
  7. //取得文件的扩展名,并转换成小写
  8. string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
  9. //验证上传文件是否图片格式
  10. fileOk = IsImage(fileExtension);
  11. if (fileOk)
  12. {
  13. //对上传文件的大小进行检测,限定文件最大不超过8M
  14. if (pic_upload.PostedFile.ContentLength < 8192000)
  15. {
  16. string filepath = "~/Admin/I_Institution/Images/";
  17. if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
  18. {
  19. Directory.CreateDirectory(Server.MapPath(filepath));
  20. }
  21. string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//这是存到服务器上的虚拟路径
  22. string mappath = Server.MapPath(virpath);//转换成服务器上的物理路径
  23. pic.Visible = true;
  24. pic_upload.PostedFile.SaveAs(mappath);//保存图片
  25. //显示图片
  26. pic.ImageUrl = virpath;
  27. lbl_pic.Visible = true;
  28. //清空提示
  29. lbl_pic.Text = "上传成功";
  30. }
  31. else
  32. {
  33. pic.Visible = false;
  34. lbl_pic.Visible = true;
  35. pic.ImageUrl = "";
  36. lbl_pic.Text = "文件大小超出8M!请重新选择!";
  37. }
  38. }
  39. else
  40. {
  41. lbl_pic.Visible = false;
  42. pic.ImageUrl = "";
  43. lbl_pic.Text = "要上传的文件类型不对!请重新选择!";
  44. }
  45. }
  46. else
  47. {
  48. lbl_pic.Visible = false;
  49. pic.ImageUrl = "";
  50. lbl_pic.Text = "请选择要上传的图片!";
  51. }
  52. #endregion
  53. }
  54. /// <summary>
  55. /// 验证是否指定的图片格式
  56. /// </summary>
  57. /// <param name="str"></param>
  58. /// <returns></returns>
  59. public bool IsImage(string str)
  60. {
  61. bool isimage = false;
  62. string thestr = str.ToLower();
  63. //限定只能上传jpg和gif图片
  64. string[] allowExtension = { ".jpg", ".gif", ".bmp", ".png" };
  65. //对上传的文件的类型进行一个个匹对
  66. for (int i = 0; i < allowExtension.Length; i++)
  67. {
  68. if (thestr == allowExtension[i])
  69. {
  70. isimage = true;
  71. break;
  72. }
  73. }
  74. return isimage;
  75. }
  76. /// <summary>
  77. /// 创建一个指定长度的随机salt值
  78. /// </summary>
  79. public string CreateSalt(int saltLenght)
  80. {
  81. //生成一个加密的随机数
  82. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  83. byte[] buff = new byte[saltLenght];
  84. rng.GetBytes(buff);
  85. //返回一个Base64随机数的字符串
  86. return Convert.ToBase64String(buff);
  87. }
  88. /// <summary>
  89. /// 返回加密后的字符串
  90. /// </summary>
  91. public string CreatePasswordHash(string pwd, int saltLenght)
  92. {
  93. string strSalt = CreateSalt(saltLenght);
  94. //把密码和Salt连起来
  95. string saltAndPwd = String.Concat(pwd, strSalt);
  96. //对密码进行哈希
  97. string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
  98. //转为小写字符并截取前16个字符串
  99. hashenPwd = hashenPwd.ToLower().Substring(0, 16);
  100. //返回哈希后的值
  101. return hashenPwd;
  102. }

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

   前台代码:

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

以上代码就是upload上传单张图片的全部代码,希望大家喜欢。

人气教程排行