【问题标题】:How to bind imageurl to its imageId stored in database?如何将 imageurl 绑定到其存储在数据库中的 imageId?
【发布时间】:2013-07-25 11:10:28
【问题描述】:

我已经上传了一张图片并将其存储到 asp.net 中的一个文件夹中,并将其描述存储在我的照片表包含以下列的数据库中

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

并将图像存储在名为“upload”的文件夹中

并且在我已将所有列绑定到数据库的网格视图中,我在项目模板中拍摄了图像并使用此代码绑定其 imageurl

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

并上传我使用此代码的图像

protected void Insert_Click(object sender, EventArgs e) { 尝试 {

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

}

【问题讨论】:

  • 服务器为图片创建的url是什么?
  • 我不知道
  • 如果您使用的是 Chrome,请右键单击损坏的图像,然后单击“复制图像 URL”。
  • 从链接中可以看出,问题在于“PhotoName”和“ExtName”字段未绑定到您的数据。您可以发布您的页面以使我们能够理解吗? :)

标签: asp.net sql-server-2008


【解决方案1】:

你能试试下面的代码sn-p吗?

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

或

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

如果它不起作用,请告诉我。

【讨论】:

【解决方案2】:

图片网址将

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

因为我已将图像路径另存为

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

Ans ProductID 存储在哪里,因此获得的 imageurl 将是这样的

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

其中 7 是 PhotoID

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2019-03-16
    • 2019-06-27
    • 2013-02-11
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多