Paolo Duhaylungsod
Paolo Duhaylungsod

Reputation: 487

How to make the download image link work?

I have this code for my viewdepositslip.aspx wherein it shows the imageurls from the folder where the uploaded files are stored:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded">
<Columns>
    <asp:BoundField DataField="Text" HeaderText="File Name" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

This is my code for uploading the images to the folder(this is from the client side, a different webform).

    protected void UploadFile(object sender, EventArgs e)
    {
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
    Response.Redirect(Request.Url.AbsoluteUri);
}

and this is the code behind. Well the delete button functions perfectly but i cannot make the download link function work. What am I missing here?

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/BankDepositUploads/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                files.Add(new ListItem(Path.GetFileName(filePath), filePath));
            }
            GridView1.DataSource = files;
            GridView1.DataBind();
        }
protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        Response.WriteFile(filePath);
        Response.End();
    }
    protected void DeleteFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        File.Delete(filePath);
        Response.Redirect(Request.Url.AbsoluteUri);
    }

Also I'm planning to see the actual image once it is shown in the gridview. As of now it has the image urls.

Upvotes: 0

Views: 1846

Answers (1)

Litisqe Kumar
Litisqe Kumar

Reputation: 2564

Your Code should also work but for any reason it might be not working so try like this way:

aspx page

 <asp:LinkButton ID="lnkDownload" runat="server" CommandName="cmd">Download</asp:LinkButton>

CS page

   protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmd")
            {
                string filePath = (sender as LinkButton).CommandArgument;
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                Response.WriteFile(filePath);
                Response.End();
            }
    }

EDIT:- Since you are using update panel then you have do postback when clicking on link button. There are many ways for doing this. I am explaining two ways. Put this code at page load

ScriptManager.GetCurrent(this).RegisterPostBackControl(this.GridView1);

Or use trigger in aspx page, below </ContentTemplate>

<Triggers>
        <asp:PostBackTrigger ControlID="GridView1" />
</Triggers>

Upvotes: 1

Related Questions