从ASP.NET中的gridview下载文件时,如何保存文件路径 [英] How do I save the file path when files are downloaded from gridview in ASP.NET

查看:44
本文介绍了从ASP.NET中的gridview下载文件时,如何保存文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个gridview,显示从SQL Server数据库下载的图像名称,图像和图像链接。我想在单独的数据库表中下载图像(点击链接按钮时)后保存数据库中的图像路径。请帮我!我需要这个尽快;(

这个图像下载webform连接到我的另一个webform-image上传,其中包含ImgPath(nvarchar255)字段。











以下是我的代码:



概述:到目前为止我有什么

1.图像和图像链接的网格视图

2.从数据库中检索网格视图

3.能够下载图片





我需要什么:

1.下载图片时 - - >将图像路径保存到sql server数据库



我尝试过:



的aspx:

So i have a gridview that displays image name,images and image link to download from the SQL server database. I want to save the image path in database once an image is downloaded (upon link button click) in a separate db table. Please help me! I need this asap ;(
This Image download webform is connected to my another webform-image upload which has ImgPath (nvarchar255) field.





Below are my codes:

Overview: What i have so far
1. Gridview of images and image links
2.Gridview is retrieved from database
3.Able to download the image


What i need:
1.When image is downloaded--> Save the image path to sql server database

What I have tried:

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



}

推荐答案

这是不可能的,服务器代码不知道浏览器是否已下载图像,如果技术上可能发生事件,您仍然不允许这样做,因为这将是一个安全问题。更进一步,这些信息对你的服务器代码来说是无用的,因为即使它确实知道客户端路径,也没有任何东西可以处理这些信息。
That's not possible, the server code has no idea if the browser has downloaded the image, and event if it was technically possible you still wouldn't be allowed to do it as it would be a security issue. Further more that information is useless to your server code anyway as even if it did know a client path there is nothing it can do with that information.


这篇关于从ASP.NET中的gridview下载文件时,如何保存文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆