免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

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

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

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

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

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

或移除,或是在其他主機上瀏覽該圖片時,就無法顯示了。第二種方法,就比較靈活了,可以用在交互性的頁面,比如校友錄,因為上傳的是整張圖片,所以只要讀取正確,就能任何主機上顯示出來。

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

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

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

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

Image_ID    int    identity(1,1)     primarykey    not null

Image_Wpath    varchar(50)        null

Image_Wpath 用來保存圖片的相對路徑

很簡單的界面(其實是有點丑。。。。):

點擊查看大圖

 

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

下面是效果圖:

點擊查看大圖

 

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

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

在項目里,有一個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è)置文件保存相對路徑  
  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. //獲得相對路徑  
  11.  }  
  12.  sdr.Close();  
  13.  Image1.ImageUrl = wpath2;      
  14. //圖片顯示路徑就是相對路徑  
  15.  Label1.Text = wpath2;    //顯示相對路徑  
  16. }  

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

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

Image_ID    int    identity(1,1)     primarykey    not null

Image_Content     image null

Image_Content以二進制形式保存圖片

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

點擊查看大圖

 

ASP.NET上傳圖片頁面和第一種方法一樣,同樣是用到FileUpload,以及一個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. }  

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

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

Default2.aspx用于讀取圖片。

點擊查看大圖

 

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(); 

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

點擊查看大圖

 

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.w3.org/TR/xhtml1  
  5. /DTD/xhtml1-transitional.dtd" 〉  
  6. 〈HTML xmlns="http://www.w3.org/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)點,個人更傾向于用第二種。因為第二種方法達(dá)到了真正的將圖片上傳到數(shù)據(jù)庫。在某些項目中,我們也可能要用到第一種方法。本例中,沒有嚴(yán)格的判斷圖片上傳的格式,學(xué)習(xí)的朋友可以自己做嚴(yán)格判斷,防止上傳的是有害文件。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP.NET中的圖片路徑問題
關(guān)于URL路徑的基本使用
在線生成圖片的網(wǎng)址
在Asp.net中圖片存儲、讀取、顯示通用方法詳解
網(wǎng)站后臺登錄地址大全
ASP.NET購物車(源碼下載)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服