2014-02-05 80 views
0

我從數據庫中讀取圖像文件的路徑,然後ineed在圖像查看器中查看它,但是當我嘗試這樣做時,圖像查看器是空的圖像路徑我存儲在數據庫是(「E:\媒體瀏覽器項目」)我不知道的步驟我應該做的或者我可以做什麼.........需要在ASP.net中的圖像查看器控件中查看圖像

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="images.aspx.cs" Inherits="images" %> 

<!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"> 


     .style6 
     { 
      text-align: left; 
      height: 61px; 
     } 
     .style7 
     { 
      height: 274px; 
      text-align: left; 
     } 
     .style8 
     { 
      text-align: center; 
      height: 42px; 
      font-family: "Arial Black"; 
      color: #000099; 
     } 
     .style9 
     { 
      height: 274px; 
      text-align: left; 
      width: 121px; 
     } 
     </style> 
</head> 
<body> 
    <form id="form2" runat="server"> 

     <table style="width: 100%; margin-left: 17px;"> 
      <tr> 
       <td align="center" bgcolor="#CC6699" class="style6" colspan="2"> 
        <br /> 
        <asp:SiteMapPath ID="SiteMapPath1" runat="server" Font-Names="Verdana" 
         Font-Size="0.8em" PathSeparator=" : "> 
         <CurrentNodeStyle ForeColor="#333333" /> 
         <NodeStyle Font-Bold="True" ForeColor="#7C6F57" /> 
         <PathSeparatorStyle Font-Bold="True" ForeColor="#5D7B9D" /> 
         <RootNodeStyle Font-Bold="True" ForeColor="#5D7B9D" /> 
        </asp:SiteMapPath> 
       </td> 
      </tr> 
      <tr> 
       <td bgcolor="#990099" class="style9"> 
        &nbsp;</td> 
       <td bgcolor="White" class="style7" valign="top"> 
        Title: 
        <asp:TextBox ID="titleTextBox" runat="server"></asp:TextBox> 
        <br /> 
        file path :<asp:TextBox ID="pathTextBox" runat="server"></asp:TextBox> 
        <br /> 
        Upload Date 
        <asp:TextBox ID="DateTextBox" runat="server"></asp:TextBox> 
        <br /> 
        <asp:Image ID="Image1" runat="server" Height="282px" Width="393px" /> 
        <br /> 
        <br /> 
        <br /> 
        <br /> 
       </td> 
      </tr> 
      <tr> 
       <td bgcolor="#990099" class="style8" colspan="2" valign="middle"> 
        &nbsp;</td> 
      </tr> 
     </table> 

    <div> 

    </div> 
    </form> 
</body> 
</html> 

和驗證碼在加載頁面

public partial class images : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SqlDataReader rdr=null; 

     SqlConnection conn=null; 

     try 
     { 

      string ID = Request.QueryString["id"]; 

      conn = new SqlConnection("Data Source=SHIMOFCIS-PC\\MYSQL;Initial Catalog=mydb;Integrated Security=SSPI"); 

      SqlCommand cmd; 
      conn.Open(); 

      cmd = new SqlCommand("select title,filepath,UploadDate from [Media] where [email protected]", conn); 

      cmd.Parameters.AddWithValue("@id", ID); 
      // rdr = cmd.ExecuteReader(); 

      // print the CustomerID of each record 
      using (var reader = cmd.ExecuteReader()) 
      { 
       if (reader.Read()) // you don't need while loop 
       { 
        pathTextBox.Text = reader["filePath"].ToString(); 
        DateTextBox.Text = reader["UploadDate"].ToString(); 
        titleTextBox.Text = reader["title"].ToString(); 
        Image1.ImageUrl = pathTextBox.Text + "\"+titleTextBox.Text+ "+".jpg"; 
       } 
      } 
     } 

     finally 
     { 
      // close the reader 
      if (rdr != null) 
      { 
       rdr.Close(); 
      } 
      // 5. Close the connection 
      if (conn != null) 
      { 
       conn.Close(); 
      } 
     } 
    } 
} 
+0

不要將路徑存儲爲物理文件路徑。你不能將它用於圖像源。使用相對或絕對網址。 – MikeSmithDev

+0

我試過,但它不工作我需要的圖像沒有出現只是損壞的圖像出現 – user3275745

+0

當您存儲爲相對URL時,結果是什麼?你的查詢返回了什麼? – MikeSmithDev

回答

0

我知道這是一段時間,因爲你發佈這個,但任何其他人發現這一點!

爲圖像使用相對路徑,然後找到它們相對於您使用Server.MapPath的aspx頁面,並添加它的相對路徑。

例如(File.Exists(Server.MapPath(「\ Data \ Competitions \ CurrentSeason \」+ round +「\ Intermediate \ Scores.xml」)))