Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im having problem on my code whenever i click the linkbutton(download) to download the uploaded file and when i click other button on the page still it downloads the file that i uploaded and here's my code

lblFile.Text = Issue.FileName;

        if (lblFile.Text != "")
        {
            trAttachedFile.Visible = true;
            lbtnDownload.PostBackUrl = "~/Download.aspx?file=" + lblFile.Text;
        }
        else
        {
            trAttachedFile.Visible = false;
        }




And here's the Download.aspx code behind



protected void Page_Load(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(Request.QueryString["file"]))
        {
            DownloadID = Request.QueryString["file"];
            if (StartDownload() == true)
            {
                lblMessage.Text = "Your download should start shortly";
            }
            else
            {
                lblMessage.Text = "Download File does not exist";
            }
        }

private bool StartDownload()
    {
        if (DownloadID != "")
        {
            string downloadPath = WebConfigurationManager.AppSettings["SubPic"].ToString() + DownloadID;
            FileInfo downloadFile = new FileInfo(downloadPath);

            if (downloadFile.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFile.Name);
                Response.AddHeader("Content-Length", downloadFile.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(downloadFile.FullName);
                Response.End();
                return true;
            }
        }
        return false;
    }




and here's the linkbutton

<tr runat="server" id="trAttachedFile">
            <td>
                <asp:Label runat="server" Text="File Attachment:" />
            </td>
            <td colspan="2">
                <asp:Label runat="server" ID="lblFile" />
                 
                <asp:LinkButton runat="server" Text="Download" ID="lbtnDownload" CssClass="lbtnDownload" />
            </td>
            <td> </td>
        </tr>


What I have tried:

Any suggestion guys on my code. Thanks in advance
Posted
Updated 8-Oct-17 20:45pm
Comments
Richard Deeming 10-Oct-17 12:15pm    
REPOST
This is the same (solved) question you asked last week:
https://www.codeproject.com/Questions/1208692/When-clicking-the-linkbutton-download-and-I-click[^]

The answer hasn't changed in the last 9 days!

1 solution

Do not write the download logic in your Page_Load event, since it will be called everytime the page is loaded, and according to your logic, will download a file if the request has the "File" key.

Write the logic in the LinkButton.Click event of the lbtnDownload. This will restrict the download action to the click event of the button and won't download every time your page is loaded.
 
Share this answer
 
v3

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