0

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()?

mason
  • 31,774
  • 10
  • 77
  • 121
user1782815
  • 195
  • 1
  • 3
  • 17
  • Why not simply a link to the file, in the window, rather than a javascript call to the file? – dodexahedron Apr 09 '15 at 18:49
  • 2
    You might try: window.location.href = "myurl/file.pdf"; – Russ Apr 09 '15 at 18:51
  • I guess you can do this setting some proper headers in your response. Take a look at this thread (PHP) http://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header, you should use Content-Disposition: attachment – Christian Benseler Apr 09 '15 at 18:54
  • @mason, the session is not transferred because the link opens in a new IE window (i.e not inside the C# WebBrowser frame). Will check out the suggestions above. – user1782815 Apr 09 '15 at 18:59
  • @mason, WinForm application. It works good without the WinForm application. – user1782815 Apr 09 '15 at 19:00
  • @Russ, using window.location.href on the button OnClick event still opens a new IE dialog with the URL. – user1782815 Apr 09 '15 at 19:03
  • @dodexahedron, when linking the file directly the dialog turns white. – user1782815 Apr 09 '15 at 19:06

2 Answers2

1

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

Tim
  • 4,051
  • 10
  • 36
  • 60
0

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.

user1782815
  • 195
  • 1
  • 3
  • 17