简体   繁体   English

直接下载文件,而不是从window.open(url)下载文件

[英]Download file directly, not from window.open(url)

I'm currently having some session issues in an ASP.NET application. 我目前在ASP.NET应用程序中遇到一些会话问题。 The main application opens an ASP.NET dialog, which contains a link to a PDF file. 主应用程序将打开一个ASP.NET对话框,其中包含一个PDF文件的链接。 This file is 'downloaded' by using window.open('myurl/file.pdf'); 通过使用window.open('myurl/file.pdf');可“下载”此文件window.open('myurl/file.pdf');

This results in a new window opening, but the file cannot be downloaded due to the session object is not transferred (keep in mind the solution is a bit more complex, so trying to keep the session in the new window will not work because it's embedded in an C# WebBrowser frame). 这会导致打开一个新窗口,但由于未传输会话对象,因此无法下载文件(请注意,解决方案要复杂一些,因此尝试将会话保留在新窗口中是无效的,因为它已嵌入在C#WebBrowser框架中)。

Are there any possibilities downloading the file directly from the link, not through window.open()? 是否有可能直接从链接而不是通过window.open()下载文件?

If the file exists on the file system, you could just link to it. 如果文件存在于文件系统中,则可以链接到它。 I know this sometimes winds up opening the file in the browser depending on the user's setup. 我知道有时候这会根据用户的设置在浏览器中打开文件。

If you don't want to do this through opening a window and the file is generated dynamically: 如果您不想通过打开窗口来执行此操作,并且文件是动态生成的:

  1. Use a Button or a LinkButton 使用ButtonLinkButton
  2. Use Response.AddHeader in the Click event of your Button/LinkButton 在Button / LinkBut​​ton的Click事件中使用Response.AddHeader

      Response.AddHeader("content-disposition", "attachment;filename={filename.extension}") Response.ContentType = "application/{MIME type here}" 
    1. Stream the results to the client (you'd need to look this up, I do it a lot with Excel by streaming DataGrids but not so much with PDFs) 将结果流式传输到客户端(您需要查询一下,我通过流式处理DataGrid来使用Excel进行很多工作,而对于PDF则做得不多)

This should prompt the user what to do... 这应该提示用户该怎么做...

This got resolved by simply calling window.dialogArguments.MyFunction(url), which invokes the parent windows MyFunction(url). 只需调用window.dialogArguments.MyFunction(url)即可解决此问题,该窗口将调用父窗口MyFunction(url)。 In that window I used window.external.MyFunctionToDotNet(url), which again took the cookies from my WebBrowser in a WebClient and downloaded the file. 在该窗口中,我使用了window.external.MyFunctionToDotNet(url),它再次从WebClient中的WebBrowser中获取cookie并下载了文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM