单击其他按钮时下载文件

时间:2017-10-05 18:59:46

标签: c# asp.net

我的代码出现问题,每当我点击链接按钮(下载)下载上传的文件,我点击另一个按钮仍然下载上传的文件,这是我的代码。

lblFile.Text = Issue.FileName;

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

这里是

背后的Download.aspx代码
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;
    }

这里是linkbutton

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

1 个答案:

答案 0 :(得分:0)

因为当您使用按钮的PostBackUrl属性时,它会更改表单的PostBackUrl

要么添加一些代码,要将PostBackUrl更改为代码中的原始代码,或者在您的情况下,您不需要真正的回发网址更改,只需尝试使用超链接导航到您的下载网址开始下载