从ASP.NET中的gridview下载文件时,如何保存文件路径

时间:2019-02-08 15:56:42

标签: c# asp.net sql-server download

我有一个gridview(webservice),它显示图像名称,图像和图像链接以从SQL Server数据库下载。下载图像(单击链接按钮后)到单独的数据库表中后,我想将图像路径保存在数据库中。

此图像下载Webform已连接到我的另一个具有ImgPath(nvarchar255)字段的Webform-image上传。

概述:到目前为止我所拥有的:

  1. 图像和图像链接的网格视图
  2. Gridview是从数据库中检索的
  3. 能够下载图像

我需要什么:

  1. 下载图像时->将图像路径保存到SQL Server数据库

aspx:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
    <Columns>

        <asp:BoundField HeaderText="ID" DataField="AdvID" />
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:BoundField HeaderText="Item" DataField="Item" />
        <asp:ImageField HeaderText="Image" DataImageUrlField="ImgPath" ControlStyle-Height="120" ControlStyle-Width="140">
            <ControlStyle Height="120px" Width="140px"></ControlStyle>
        </asp:ImageField>
        <asp:TemplateField HeaderText="View Information">
            <ItemTemplate>
                <asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%#Eval("AdvID") %>' OnClick="lnk_OnClick">View</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="DownloadLink">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Item") %>' Text='<%# Eval("Item") %>' CommandName="Download"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

aspx.cs:

public partial class AdvGridView : System.Web.UI.Page
{

SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {

        Response.Clear();
        Response.ContentType = "application/octect-stream";
        Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
        Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
        Response.End();


    }
}
void FillGridView()
{
    if (sqlCon.State == ConnectionState.Closed)
        sqlCon.Open();
    SqlDataAdapter sqlDa = new SqlDataAdapter("ViewAll", sqlCon);
    sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
    DataTable dtbl = new DataTable();
    sqlDa.Fill(dtbl);
    sqlCon.Close();
    GridView1.DataSource = dtbl;
    GridView1.DataBind();
}

protected void lnk_OnClick(object sender, EventArgs e)
{
    int AdvertisementID = Convert.ToInt32((sender as LinkButton).CommandArgument);
    if (sqlCon.State == ConnectionState.Closed)
        sqlCon.Open();
    SqlDataAdapter sqlDa = new SqlDataAdapter("ViewByID", sqlCon);
    sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlDa.SelectCommand.Parameters.AddWithValue("@AdvID", AdvertisementID);
    DataTable dtbl = new DataTable();
    sqlDa.Fill(dtbl);

    sqlCon.Close();

}

private void BindGrid()
{
    GridViewService.WebService service = new GridViewService.WebService();
    GridView1.DataSource = service.Get();
    GridView1.DataBind();
}
}

0 个答案:

没有答案