這篇文章主要介紹了upload上傳單張圖片的代碼,需要的朋友可以參考下。
通過Upload上傳單張圖片,具體實現(xiàn)方式請看代碼。
- protected void btnpic_upload_Click(object sender, EventArgs e)
- {
- #region 上傳文件
- Boolean fileOk = false;
- if (pic_upload.HasFile)//驗證是否包含文件
- {
- //取得文件的擴(kuò)展名,并轉(zhuǎn)換成小寫
- string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
- //驗證上傳文件是否圖片格式
- fileOk = IsImage(fileExtension);
- if (fileOk)
- {
- //對上傳文件的大小進(jìn)行檢測,限定文件最大不超過8M
- if (pic_upload.PostedFile.ContentLength < 8192000)
- {
- string filepath = "~/Admin/I_Institution/Images/";
- if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就創(chuàng)建file文件夾
- {
- Directory.CreateDirectory(Server.MapPath(filepath));
- }
- string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//這是存到服務(wù)器上的虛擬路徑
- string mappath = Server.MapPath(virpath);//轉(zhuǎn)換成服務(wù)器上的物理路徑
- 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" };
- //對上傳的文件的類型進(jìn)行一個個匹對
- for (int i = 0; i < allowExtension.Length; i++)
- {
- if (thestr == allowExtension[i])
- {
- isimage = true;
- break;
- }
- }
- return isimage;
- }
- /// <summary>
- /// 創(chuàng)建一個指定長度的隨機(jī)salt值
- /// </summary>
- public string CreateSalt(int saltLenght)
- {
- //生成一個加密的隨機(jī)數(shù)
- RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
- byte[] buff = new byte[saltLenght];
- rng.GetBytes(buff);
- //返回一個Base64隨機(jī)數(shù)的字符串
- return Convert.ToBase64String(buff);
- }
- /// <summary>
- /// 返回加密后的字符串
- /// </summary>
- public string CreatePasswordHash(string pwd, int saltLenght)
- {
- string strSalt = CreateSalt(saltLenght);
- //把密碼和Salt連起來
- string saltAndPwd = String.Concat(pwd, strSalt);
- //對密碼進(jìn)行哈希
- string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
- //轉(zhuǎn)為小寫字符并截取前16個字符串
- hashenPwd = hashenPwd.ToLower().Substring(0, 16);
- //返回哈希后的值
- return hashenPwd;
- }
拿到上傳后的圖片路徑:
復(fù)制代碼代碼如下:
- string IconUrl = this.pic.ImageUrl.Trim();
- model.IconUrl = Path.GetFileName(IconUrl); //獲得已上傳 圖片控件的URL
前臺代碼:
- tr>
- <td height="25" width="30%" align="right">
- 機(jī)構(gòu)圖標(biāo)路徑 :
- </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上傳單張圖片的全部代碼,希望大家喜歡。
新聞熱點
疑難解答
圖片精選