直接链接到asp.net中的数据库中的图像

时间:2013-03-31 06:14:43

标签: asp.net sql image

我们如何通过直接链接从数据库中检索图像?

据我所知,我们可以将图片设为/getImage.aspx?id=1

但是我们如何创建像/image1.jpg这样的直接链接?

2 个答案:

答案 0 :(得分:1)

您可以使用URL Rewrite来解决问题

答案 1 :(得分:0)

YOU SHOULD TRY THIS

 <asp:GridView ID="GridView1" runat="server" EnableViewState="false"
    AutoGenerateColumns="false"> 
   <Columns> 
    <asp:BoundField HeaderText="AutoId" DataField="AutoId" /> 
     <asp:BoundField HeaderText="File Name" DataField="FileName" />  
      <asp:TemplateField HeaderText="File"> 
      <ItemTemplate> <img src="ShowImage.ashx?autoId=<%# Eval("AutoId") %>" alt="<%# 
       Eval("FileName") %>" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="IsActive"> 
    <ItemTemplate> <%# Eval("Active").ToString().Equals("True") ? "Yes" : "No" %> 
     </ItemTemplate> 
      </asp:TemplateField> 
    </Columns> 
     </asp:GridView> 

   Behind Code: 
   string _connStr =
   ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 
    protected void Page_Load(object sender, EventArgs e) 
  { 
     if (!IsPostBack) 
       { 
           BindFiles(); 
       } 
   } 

        private void BindFiles() 
        { 
          DataTable table = new DataTable(); 

         using (SqlConnection conn = new SqlConnection(_connStr)) 
             { 

                string sql = "SELECT AutoId, FileName, FileContent, Active FROM Files
                Order By AutoID ASC"; 

            using (SqlCommand cmd = new SqlCommand(sql, conn)) 
                { 

               using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) 
                      { 
                          ad.Fill(table); 
                      } 

                 } 
                } 
           GridView1.DataSource = table; 
            GridView1.DataBind(); 
           }