乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片...

       狼志凌云 2010-10-29

      今天,和大家討論一下在ASP.NET中,如何上傳圖片至數(shù)據(jù)庫,然后再將圖片讀取顯示的問題。歡迎高手提出自己的方法?。。?/p>

      目前,我主要用到以下兩種方法:

      1:上傳圖片的相對(duì)路徑到數(shù)據(jù)庫中相應(yīng)字段里,讀取顯示時(shí),將控件(假設(shè)用的是Image控件)的ImageUrl屬性指向該相對(duì)路徑即可。

      2:將圖片以二進(jìn)制流的方式整體上傳到數(shù)據(jù)庫里,讀取顯示時(shí),以二進(jìn)制流的方式整體讀出。這種方法稍微麻煩一點(diǎn),但保存的是圖片整體到數(shù)據(jù)庫里。

      第一種方法,實(shí)現(xiàn)起來比較簡單,因?yàn)榇嫒霐?shù)據(jù)庫里的只是圖片相對(duì)路徑,當(dāng)然,同時(shí)也就有很大的局限性,由于是相對(duì)路徑,所以當(dāng)本地的圖片變換了位置

      或移除,或是在其他主機(jī)上瀏覽該圖片時(shí),就無法顯示了。第二種方法,就比較靈活了,可以用在交互性的頁面,比如校友錄,因?yàn)樯蟼鞯氖钦麖垐D片,所以只要讀取正確,就能任何主機(jī)上顯示出來。

      下面,分別通過實(shí)際的代碼,介紹這兩種方法。

      在這兩個(gè)方法里,我將用到一個(gè)控件:FileUpload,該控件的具體用法參見百度谷歌。。。學(xué)習(xí)過程中,最好的老師就是他們倆。

      1:ASP.NET上傳圖片上傳圖片相對(duì)路徑,并讀取顯示。

      數(shù)據(jù)庫里的字段很簡單,就兩個(gè)

      Image_ID    int    identity(1,1)     primarykey    not null

      Image_Wpath    varchar(50)        null

      Image_Wpath 用來保存圖片的相對(duì)路徑

      很簡單的界面(其實(shí)是有點(diǎn)丑。。。。):

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片(圖一)

      點(diǎn)擊查看大圖

       

      這里注意,我需要上傳的文件都放在文件夾“Image”,在后面的上傳路徑里就需要這個(gè)文件夾。

      下面是效果圖:

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片(圖二)

      點(diǎn)擊查看大圖

       

      我在輸入框里填入Image_ID的值,讀取指定的圖片,在圖片的下面,顯示出該圖片的相對(duì)路徑。

      接下來,我們看一下具體代碼實(shí)現(xiàn)上傳和讀取顯示功能。

      在項(xiàng)目里,有一個(gè)sqlHelper類,是一些常用的數(shù)據(jù)訪問方法。這里就不詳細(xì)講了。

      上傳按鈕里的事件:

      CODE:

      1. protected void Button1_Click(object sender, EventArgs e)  
      2. {  
      3. string name = FileUpload1.FileName;//獲取文件名  
      4. string type = name.Substring(name.LastIndexOf(".") + 1);     
      5.  //獲取文件類型  
      6. string ipath = Server.MapPath("Image") + "\\" + name;     
      7.  //獲取文件路徑  
      8. string wpath = "Image\\" + name;   
      9. //[color=red]設(shè)置文件保存相對(duì)路徑  
      10. (這里的路徑起始就是我們存放圖片的文件夾名)[/color]  
      11.  
      12. string query1 = "insert into Images values 
      13. ('" + wpath + "')";  
      14.  
      15. if (type == "jpg" || type == "gif" ||   
      16. type == "bmp" || type == "png")  
      17. {  
      18.   FileUpload1.SaveAs(ipath); //服務(wù)器保存路徑  
      19.   sqlHelper.ExecterNonQuery(query1);  
      20. }  

      顯示按鈕事件:

      CODE:

      1. protected void Button2_Click(object sender, EventArgs e)  
      2. {  
      3.  string query2 = "select * from Images where   
      4. Image_ID=" + Convert.ToInt32(TextBox1.Text);  
      5.  SqlDataReader sdr = sqlHelper.GetReader(query2);  
      6.  string wpath2 = "";  
      7.  while (sdr.Read())  
      8.  {  
      9.  wpath2 = sdr[1].ToString();      
      10. //獲得相對(duì)路徑  
      11.  }  
      12.  sdr.Close();  
      13.  Image1.ImageUrl = wpath2;      
      14. //圖片顯示路徑就是相對(duì)路徑  
      15.  Label1.Text = wpath2;    //顯示相對(duì)路徑  
      16. }  

      2:以二進(jìn)制流的方式存入數(shù)據(jù)庫,并讀取顯示圖片。

      數(shù)據(jù)庫的字段同樣簡單:

      Image_ID    int    identity(1,1)     primarykey    not null

      Image_Content     image null

      Image_Content以二進(jìn)制形式保存圖片

      整體看一下例子里的頁面組成:

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片(圖三)

      點(diǎn)擊查看大圖

       

      ASP.NET上傳圖片頁面和第一種方法一樣,同樣是用到FileUpload,以及一個(gè)Button,具體代碼如下:

      CODE:

      1. protected void Button1_Click(object sender, EventArgs e)  
      2. {  
      3. string name = FileUpload1.PostedFile.FileName;  
      4. string type = name.Substring(name.LastIndexOf(".") + 1);   
      5. FileStream fs = File.OpenRead(name);  
      6. byte[] content = new byte[fs.Length];  
      7. fs.Read(content, 0, content.Length);  
      8. fs.Close();  
      9.  
      10. SqlConnection conn = new SqlConnection("Data   
      11. Source=;Initial Catalog=;Persist Security   
      12. Info=True;User ID=;Pooling=False;Password=");  
      13. SqlCommand cmd = conn.CreateCommand();  
      14. conn.Open();  
      15. cmd.CommandText = "insert into Images  
      16. (Image_Content) values (@content)";  
      17. cmd.CommandType = CommandType.Text;  
      18.  
      19. if (type == "jpg" || type == "gif" ||   
      20. type == "bmp" || type == "png")  
      21. {  
      22. SqlParameter para = cmd.Parameters.Add 
      23. ("@content", SqlDbType.Image);  
      24. para.Value = content;  
      25. cmd.ExecuteNonQuery();  
      26. }  
      27. }  

      顯示一張圖片和顯示一組圖片有所不同,將會(huì)分別說明之。

      顯示一張圖片,要用到Default.aspx和Default2.aspx。Default.aspx中有一個(gè)控件Image,它的屬性ImageUrl指向Default2.aspx用于顯示圖片。

      Default2.aspx用于讀取圖片。

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片(圖四)

      點(diǎn)擊查看大圖

       

      Default.aspx.cs

      CODE:

      1. protected void Page_Load(object sender, EventArgs e)  
      2. {  
      3. Image1.ImageUrl = "Default2.aspx";  
      4. }  

      Default2.aspx.cs

      CODE:

      1. string imgid = Request.QueryString["imgid"];  
      2. SqlConnection conn1 = new SqlConnection  
      3. ("Data Source=;Initial Catalog=;Persist Security   
      4. Info=True;User ID=sa;Pooling=False;Password=");  
      5. SqlCommand cmd1 = new SqlCommand("select Image_Content   
      6. from Images where Image_ID=3", conn1);       
      7. //固定顯示Image_ID為3的圖片  
      8. conn1.Open();  
      9. SqlDataReader sdr = cmd1.ExecuteReader();  
      10. if (sdr.Read())  
      11. {  
      12. Response.BinaryWrite((byte[])sdr["Image_Content"]);  
      13. }  
      14. Response.End(); 

      顯示一組圖片時(shí),用ashx Handle存放圖片。同時(shí)用GridView以列顯示圖片。Image控件綁定Image_ID。

      ASP.NET上傳圖片至數(shù)據(jù)庫并顯示圖片(圖五)

      點(diǎn)擊查看大圖

       

      allimage.aspx

      CODE:

      1. 〈%@ Page Language="C#" AutoEventWireup="true"   
      2. CodeFile="allimage.aspx.cs" Inherits="allimage" % 〉  
      3. 〈!DOCTYPE html PUBLIC "-//W3C//DTD XHTML   
      4. 1.0 Transitional//EN" "http://www./TR/xhtml1  
      5. /DTD/xhtml1-transitional.dtd" 〉  
      6. 〈HTML xmlns="http://www./1999/xhtml" 〉   
      7. 〈HEAD runat="server" 〉   
      8. 〈title 〉BindImg〈/title 〉   
      9. 〈/HEAD 〉   
      10. 〈body 〉   
      11. 〈form id="Form1" method="post" runat="server" 〉   
      12. 〈FONT face="宋體" 〉   
      13. 〈asp:DataGrid id="MyDataGrid" runat="server"   
      14. AutoGenerateColumns="False" Width="632px" 〉   
      15. 〈AlternatingItemStyle BackColor="Beige" 〉  
      16. 〈/AlternatingItemStyle 〉   
      17. 〈HeaderStyle HorizontalAlign="Center" 〉  
      18. 〈/HeaderStyle 〉   
      19. 〈Columns 〉   
      20. 〈asp:TemplateColumn HeaderText="Photo" 〉   
      21. 〈ItemTemplate 〉 〈asp:Image ID="Image1" runat="server"   
      22. Height="70px" ImageUrl='  
      23. 〈%# "Getimg.ashx?id="+Eval("Image_ID") % 〉'  
      24. Width="100px" / 〉  
      25. 〈/ItemTemplate 〉   
      26. 〈/asp:TemplateColumn 〉   
      27. 〈/Columns 〉   
      28. 〈/asp:DataGrid 〉〈/FONT 〉   
      29. 〈asp:SqlDataSource ID="SqlDataSource1" runat="server"   
      30. ConnectionString="  
      31. 〈%$ ConnectionStrings:PracticeConnectionString % 〉"  
      32. SelectCommand="SELECT * FROM [Images]" 〉  
      33. 〈/asp:SqlDataSource 〉  
      34. 〈/form 〉   
      35. 〈/body 〉   
      36. 〈/HTML 〉  

      allimage.aspx.cs

      CODE:

      1. protected void Page_Load(object sender, EventArgs e)  
      2. {  
      3. MyDataGrid.DataSource = SqlDataSource1;  
      4. MyDataGrid.DataBind();  
      5. }  

      Getimg.ashx

      CODE:

      1. 〈%@ WebHandler Language="C#" Class="Getimg" %〉  
      2.  
      3. using System;  
      4. using System.Web;  
      5. using System.Data;  
      6. using System.Data.SqlClient;  
      7. using System.Configuration;  
      8.  
      9. public class Getimg : IHttpHandler {  
      10. public void ProcessRequest (HttpContext context)  
      11. {  
      12. int id = int.Parse(context.Request.QueryString["id"]);  
      13. SqlConnection conn = new SqlConnection  
      14. (@"server=;database=;uid=;pWD=");  
      15. SqlCommand cmd = new SqlCommand  
      16. ("select Image_Content from   
      17. Images where Image_ID='" + id + "'", conn);  
      18. cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;  
      19. conn.Open();  
      20. SqlDataReader dr = cmd.ExecuteReader();  
      21. if (dr.Read())  
      22. {  
      23. context.Response.BinaryWrite((byte[])dr  
      24. ["Image_Content"]);   
      25. }  
      26. dr.Close();  
      27. }  
      28. public bool IsReusable {  
      29. get {  
      30. return false;  
      31. }  
      32. }  
      33. }  

      總結(jié):

      兩種ASP.NET上傳圖片及讀取顯示方法,各有優(yōu)點(diǎn),個(gè)人更傾向于用第二種。因?yàn)榈诙N方法達(dá)到了真正的將圖片上傳到數(shù)據(jù)庫。在某些項(xiàng)目中,我們也可能要用到第一種方法。本例中,沒有嚴(yán)格的判斷圖片上傳的格式,學(xué)習(xí)的朋友可以自己做嚴(yán)格判斷,防止上傳的是有害文件。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多