2013-07-25 46 views
1

我已經在數據庫中上載的圖像,並將其存儲在asp.net一個文件夾,存儲其描述我的照片表包含下列列如何將imageurl綁定到存儲在數據庫中的imageId中?

ProductPhoto 
Column Name Data Type Constraint 
PhotoID  Int   Primary key,auto increment 
PhotoName Varchar(100)  
ExtName  Varchar(100)  
PhotoType Varchar(100)  
PhotoSize Int 
ProductID Int   Foreign key with product info 

,並存儲在文件夾命名爲圖像「上載」

而且在這我已綁定的所有列到數據庫的GridView控件我有一個項目模板拍攝的圖像,並和使用此代碼

<asp:GridView ID="gridview" AutoGenerateColumns="False" runat="server" style="margin-left: 0px" AllowPaging="True" AllowSorting="True" CellPadding="3" Height="238px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="Solid" BorderWidth="1px" CellSpacing="2"> 
    <Columns> 
     <asp:BoundField HeaderText="Photo ID" DataField="PhotoID" /> 
     <asp:BoundField HeaderText="Photo Name" DataField="PhotoName" /> 
     <asp:BoundField HeaderText="Extention Name" DataField="ExtName" /> 
     <asp:BoundField HeaderText="Photo Type" DataField="PhotoType" /> 
     <asp:BoundField HeaderText="Photo Size" DataField="PhotoSize" /> 
     <asp:BoundField HeaderText="Product ID" DataField="ProductID" /> 
     <asp:TemplateField HeaderText="Product Image"> 
      <ItemTemplate> 
       <asp:Image ID="productimg" Height="108px" ImageUrl="~/upload/" Width="98px" runat="server"/> 
       </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Delete Record"> 
      <ItemTemplate> 
       <asp:Button ID="delete" OnClientClick="return confirm('Are You Sure To Delete The Record?')" Text="Delete This Record" CommandName="del" CommandArgument='<%# Eval("PhotoID") %>' runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Edit Record"> 
      <ItemTemplate> 
       <asp:Button ID="update" CommandName="upd" Text="Edit this Record" CommandArgument='<%# Eval("PhotoID") %>' runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
    <SortedAscendingCellStyle BackColor="#FFF1D4" /> 
    <SortedAscendingHeaderStyle BackColor="#B95C30" /> 
    <SortedDescendingCellStyle BackColor="#F1E5CE" /> 
    <SortedDescendingHeaderStyle BackColor="#93451F" /> 
    </asp:GridView> 

和上傳圖像結合其IMAGEURL我已經使用這個代碼

保護無效Insert_Click(對象發件人,EventArgs的){ 嘗試 {

if (photoupload.HasFile) 
    { 
     ProductPhoto prdctphoto = new ProductPhoto(); 
     prdctphoto.PhotoName = photoupload.FileName; 
     prdctphoto.PhotoSize = photoupload.PostedFile.ContentLength; 
     prdctphoto.PhotoType = photoupload.PostedFile.ContentType; 
     prdctphoto.ExtName = prdctphoto.PhotoName.Substring(prdctphoto.PhotoName.LastIndexOf(".") + 1); 
     prdctphoto.ProductID = int.Parse(ddlproductname.SelectedValue); 
     //Response.Write(data.ExtName); 
     int ans = new InsertAction().InsertData(prdctphoto); 
     if (ans != 0) 
     { 
      string path = Server.MapPath("~/upload/") + ans.ToString() + "." + prdctphoto.ExtName; 
      photoupload.SaveAs(path); 
      lblmsg.Text=" File is Uploaded "; 
     } 

     else 
     { 
      lblmsg.Text="Please check all the fields"; 
     } 

    } 

} 
catch (Exception ex) 
{ 
    Response.Write(ex.Message); 
} 

}

+1

服務器爲圖像創建的URL是什麼? – Alesanco

+0

我不知道 – VJain

+0

如果您使用的是Chrome瀏覽器,請右鍵點擊已損壞的圖片,然後點擊「複製圖片網址」。 – Alesanco

回答

2

可以請你下面的代碼片段試試?

ImageUrl='<%# "~/upload/" +Eval("PhotoName").ToString().Trim()+"." + Eval("ExtName").ToString().Trim() %>' 

OR

ImageUrl='<%# Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port) + "/upload/" +Eval("PhotoName")+"." + Eval("ExtName") %>' 

讓我知道,如果它不能正常工作。

+0

沒有先生它不能正常工作 – VJain

+0

你可以請提供生成的網址和實際的圖像存在的URL? –

+0

genrated url只是頁面名稱http:// localhost:1033/admin/ProductPhoto.aspx而且圖像存在於文件夾「〜/ upload /」 – VJain

0

圖像的URL將

ImageUrl= '<%#"~/upload/" + Eval("PhotoID").ToString().Trim() + "." + Eval("ExtName").ToString().Trim() %>' 

bcoz我有保存圖像路徑作爲

int ans = new InsertAction().InsertData(prdctphoto); 
     if (ans != 0) 
     { 
      string path = Server.MapPath("~/upload/") + ans.ToString() + "." + prdctphoto.ExtName; 
} 

其中ANS的ProductID被存儲和therfore所獲得IMAGEURL會是這樣

http://localhost:1033/upload/7.jpg 

其中7是PhotoID

相關問題