单击其他按钮时下载文件 [英] Downloading the file when clicking other button

查看:79
本文介绍了单击其他按钮时下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击链接按钮(下载)下载上传的文件时,我的代码都有问题,当我点击页面上的其他按钮时,它仍会下载我上传的文件,这是我的代码



 lblFile.Text = Issue.FileName; 

if(lblFile.Text!=)
{
trAttachedFile.Visible = true;
lbtnDownload.PostBackUrl =〜/ Download.aspx?file =+ lblFile.Text;
}
其他
{
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 =你的下载应该很快开始;
}
else
{
lblMessage.Text =下载文件不存在;
}
}

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();
返回true;
}
}
返回false;
}









这里是linkbutton



< tr runat =serverid =trAttachedFile> 
< td>
< asp:Label runat =serverText =File Attachment:/>
< / td>
< td colspan =2>
< asp:Label runat =serverID =lblFile/>
 
< asp:LinkBut​​ton runat =serverText =DownloadID =lbtnDownloadCssClass =lbtnDownload/>
< / td>
< td> < / td>
< / tr>





我尝试过:



我的代码上有任何建议。在此先感谢

解决方案

不要在Page_Load事件中编写下载逻辑,因为每次加载页面时都会调用它,并根据您的逻辑,将下载如果请求具有文件键,则为文件。



在lbtnDownload的LinkBut​​ton.Click事件中写入逻辑。这会将下载操作限制为按钮的单击事件,并且不会在每次加载页面时下载。


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

解决方案

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.


这篇关于单击其他按钮时下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆