简体   繁体   中英

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

I'm currently having some session issues in an ASP.NET application. The main application opens an ASP.NET dialog, which contains a link to a PDF file. This file is 'downloaded' by using 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).

Are there any possibilities downloading the file directly from the link, not through 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
  2. Use Response.AddHeader in the Click event of your Button/LinkButton

      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)

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). In that window I used window.external.MyFunctionToDotNet(url), which again took the cookies from my WebBrowser in a WebClient and downloaded the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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