Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
    }



}
Posted
Updated 8-Feb-19 4:55am
v2

1 solution

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.
 
Share this answer
 
Comments
Member 14144306 8-Feb-19 11:08am    
Thanks for your reply. Is it possible to disable the download link button once its clicked? Like cannot download again for the second time? Or when click the link button, it shows the image is already downloaded in database
F-ES Sitecore 8-Feb-19 12:13pm    
The problem with things like that is identifying the user is a return user and has already downloaded the file. Even if you require people to log in can they simply create new accounts? Also the server code has no way of knowing if the file was downloaded. The user might have elected to not download it, or to download it but the internet dropped half-way though or whatever.
Member 14144306 8-Feb-19 21:10pm    
Oh i'm not too sure how this works but my lecturer asked me to make it webservice and i did. Not sure if it can be done with webservice.
F-ES Sitecore 9-Feb-19 16:30pm    
It can't, no. Those who can, do. Those who can't, teach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900