2017-09-05 144 views
-1

我想提供一个链接按钮(asp)与附加到它在C#中的点击功能。当用户点击链接按钮后端代码发送一个PDF文件给用户下载。下载文件点击Linkbutton asp.net

我已经完成了代码,它似乎在打开页面时下载文件。但在我的一个案例中,它将打开页面作为Windows对话框。

在这种情况下,当我点击linkbutton代码执行,但没有发生下载操作。在使用windows.open打开的“窗口”对话框中加载相同的页面时,文档的下载失败。

如:父页:Dashboard.aspx 子页面:DownloadFormatSelector.aspx 我浏览到localhost/DownloadFormatSelector.aspx页面并点击“下载”按钮,浏览器(IE)显示选项“另存为”,”打开”。

但是,如果我使用window.showModalDialog从仪表板打开DownloadFormatSelector.aspx页面。下载不会发生 例如:使用window.showModalDialog打开localhost/Dashboard.aspx和DownloadFormatSelector.aspx页面。

我被困为什么当页面呈现在window.showModalDialog里面时,它不下载文档或显示另存为选项。

以下所有代码都存在于DownloadFormatSelector文件中。

Dashboard.aspx只是父窗口。我能够执行下载时,子窗口导航到(像localhost/DownloadFormatSelector.aspx),但同一页面时打开window.showModalDialog内我无法下载文件有页面不显示任何保存为或打开下面的选项。我有什么可以修改执行页面上保存的showModalDialog内

代码C#点击:

public void DownloadButton_Click(Object sender, EventArgs e) 
{ 
    byte[] data=GetData(); 
    Response.Clear(); 
    Response.AddHeader("Content-Disposition", 
    String.Format("attachment;filename={0}", "temp.pdf"));    
    Response.ContentType = "application/pdf"; 
    Response.BinaryWrite(data); 
    Response.End(); 
} 

Asp.Net

<asp:LinkButton id="Link" Text="Download" OnClick="DownloadButton_Click" runat="server"/> 

在窗口中打开对话框:

window.showModalDialog JS method to call another aspx page to be rendered inside the dialog window 
+0

你是什么意思的“打开页面作为一个Windows对话框”?这段代码应该向客户端发送一个文件,但不清楚问题是什么。 – David

+0

嗨大卫, 我打开类似于这篇文章的aspx页面window.open('child_page.html','name','width = 200,height = 200'); ..从这个链接https:// stackoverflow.com/questions/5660700/javascript-to-open-popup-window-and-disable-parent-window –

+0

“页面在窗口对话框中加载”。什么窗口对话框?你的意思是你收到一条消息,询问你是否打开或保存它? – ADyson

回答

0

您正在寻找Response.TransmitFile。下面是你如何做到这一点:

public void DownloadButton_Click(Object sender, EventArgs e) 
{ 
    var filePath = "path to your file"; 
    Response.ContentType = "application/octet-stream"; 
    Response.AppendHeader("Content-Disposition","attachment; filename=whatever.ext"); 
    Response.TransmitFile(filePath); 
    Response.End(); 
} 
+0

嗨Bozhidar,感谢您的代码。但我问题是关于JS window.showModalDialog不让子窗口显示保存为或打开选项 –