ASP.NET圖片上傳實(shí)例(附源碼)
由于需要圖片上傳的功能,所以花了一些時(shí)間網(wǎng)上找相關(guān)資料終于搞定,效果圖如下:
下面的是解決方案截圖和上傳的圖片截圖:
下面是代碼:
1.界面代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>圖片上傳和顯示</title> <style type="text/css"> .pic_text{ color:Red;} .pic_label { color:Gray; margin-top:5px; margin-bottom:5px;} .pic_image { margin:5px;} </style> </head> <body> <form id="form1" runat="server"> <div class="pic_image"><asp:Image ID="pic" runat="server" /></div> <div><asp:FileUpload ID="pic_upload" runat="server" /><asp:Label ID="lbl_pic" runat="server" class="pic_text"></asp:Label></div> <div class="pic_label">上傳圖片格式為.jpg, .gif, .bmp,.png,圖片大小不得超過(guò)8M</div> <div><asp:Button ID="btn_upload" runat="server" Text="上傳" onclick="btn_upload_Click"/></div> </form> </body> </html>
2.后臺(tái)代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Security.Cryptography; using System.Web.Security; namespace Pic_Try { public partial class UploadPic : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_upload_Click(object sender, EventArgs e) { Boolean fileOk = false; if (pic_upload.HasFile)//驗(yàn)證是否包含文件 { //取得文件的擴(kuò)展名,并轉(zhuǎn)換成小寫 string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower(); //驗(yàn)證上傳文件是否圖片格式 fileOk = IsImage(fileExtension); if (fileOk) { //對(duì)上傳文件的大小進(jìn)行檢測(cè),限定文件最大不超過(guò)8M if (pic_upload.PostedFile.ContentLength < 8192000) { string filepath = "/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_upload.PostedFile.SaveAs(mappath);//保存圖片 //顯示圖片 pic.ImageUrl = virpath; //清空提示 lbl_pic.Text = ""; } else { pic.ImageUrl = ""; lbl_pic.Text = "文件大小超出8M!請(qǐng)重新選擇!"; } } else { pic.ImageUrl = ""; lbl_pic.Text = "要上傳的文件類型不對(duì)!請(qǐng)重新選擇!"; } } else { pic.ImageUrl = ""; lbl_pic.Text = "請(qǐng)選擇要上傳的圖片!"; } } /// <summary> /// 驗(yàn)證是否指定的圖片格式 /// </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" }; //對(duì)上傳的文件的類型進(jìn)行一個(gè)個(gè)匹對(duì) for (int i = 0; i < allowExtension.Length; i++) { if (thestr == allowExtension[i]) { isimage = true; break; } } return isimage; } /// <summary> /// 創(chuàng)建一個(gè)指定長(zhǎng)度的隨機(jī)salt值 /// </summary> public string CreateSalt(int saltLenght) { //生成一個(gè)加密的隨機(jī)數(shù) RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] buff = new byte[saltLenght]; rng.GetBytes(buff); //返回一個(gè)Base64隨機(jī)數(shù)的字符串 return Convert.ToBase64String(buff); } /// <summary> /// 返回加密后的字符串 /// </summary> public string CreatePasswordHash(string pwd, int saltLenght) { string strSalt = CreateSalt(saltLenght); //把密碼和Salt連起來(lái) string saltAndPwd = String.Concat(pwd, strSalt); //對(duì)密碼進(jìn)行哈希 string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1"); //轉(zhuǎn)為小寫字符并截取前16個(gè)字符串 hashenPwd = hashenPwd.ToLower().Substring(0, 16); //返回哈希后的值 return hashenPwd; } } }
3.最后防止上傳大文件圖片時(shí)報(bào)錯(cuò),配置文件添加配置
<?xml version="1.0" encoding="utf-8"?> <!-- 如何配置 ASP.NET 應(yīng)用程序的詳細(xì)消息 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime executionTimeout="240" maxRequestLength="8192000"/> </system.web> </configuration>
ASP.NET圖片自動(dòng)上傳和局部刷新顯示的源碼下載。
希望大家喜歡這篇文章。
相關(guān)文章
asp.net 文件上傳與刷新與asp.net頁(yè)面與iframe之間的數(shù)據(jù)傳輸
眾所周知微軟所提供的updatepanel不能支持文件上傳的異步刷新,但是往往當(dāng)你在項(xiàng)目中的其他頁(yè)面實(shí)現(xiàn)了異步刷新之后,客戶就會(huì)問(wèn)你為什么有文件上傳的頁(yè)面就不能實(shí)現(xiàn)異步刷新呢?這時(shí)我們可能說(shuō)一堆理由,但是最后大部分還是會(huì)妥協(xié)于客戶。2009-12-12ASP.NET中的跳轉(zhuǎn) 200, 301, 302轉(zhuǎn)向?qū)崿F(xiàn)代碼
跳轉(zhuǎn)非常常用,在哪里都一樣,這里的一些說(shuō)明和用法也如此,不止適用于asp.net,其他語(yǔ)言也會(huì)用得到。跳轉(zhuǎn)的目的本來(lái)很簡(jiǎn)單,就是當(dāng)用戶或系統(tǒng)需要時(shí)從一個(gè)頁(yè)面轉(zhuǎn)向另一個(gè)頁(yè)面,但自從有了各種各樣的需求,還有那個(gè)什么SEO的東西之后,跳轉(zhuǎn)被搞得極其復(fù)雜2008-09-09在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請(qǐng)求
這篇文章主要介紹了在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請(qǐng)求的方法,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03.Net 調(diào)用存儲(chǔ)過(guò)程取到return的返回值
存儲(chǔ)過(guò)程只能返回 int 類型,如果返回一個(gè)字符串 ,將會(huì)報(bào)類型轉(zhuǎn)化錯(cuò)誤,下面以示例介紹下如何取到return的值,需要的朋友可以參考下2014-08-08ASP.NET MVC5驗(yàn)證系列之Fluent Validation
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之Fluent Validation,感興趣的小伙伴們可以參考一下2016-07-07.NET實(shí)現(xiàn)可交互的WINDOWS服務(wù)的實(shí)例代碼
那么來(lái)看一下如何才能實(shí)現(xiàn)一個(gè)可交互的服務(wù)呢。步驟與實(shí)現(xiàn)基本的服務(wù)一樣2013-03-03深入理解__doPostBack 客戶端調(diào)用服務(wù)端事件
__doPostBack是一個(gè)純粹并且是非常簡(jiǎn)單的javascript函數(shù),大部分的頁(yè)面PostBack都是由它觸發(fā)的。2008-08-08剖析ASP.NET MVC的DependencyResolver組件
這篇文章主要為大家剖析ASP.NET MVC的DependencyResolver組件,感興趣的小伙伴們可以參考一下2016-04-04