簡體   English   中英

單擊其他按鈕時下載文件

[英]Downloading the file when clicking the other button

我的代碼有問題,每當我單擊鏈接按鈕(下載)以下載上載的文件,然后單擊另一個按鈕仍下載該上載的文件時,這就是我的代碼。

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

這是鏈接按鈕

<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>

因為當您使用按鈕的PostBackUrl屬性時,它將更改表單的PostBackUrl

添加一些代碼以將PostBackUrl更改回代碼中的原始位置,或者在某些情況下,您不需要真正更改回發URL,只需嘗試使用超鏈接導航到您的下載URL即可啟動下載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM