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

7
  • Why not simply a link to the file, in the window, rather than a javascript call to the file? Commented Apr 9, 2015 at 18:49
  • 2
    You might try: window.location.href = "myurl/file.pdf"; Commented Apr 9, 2015 at 18:51
  • I guess you can do this setting some proper headers in your response. Take a look at this thread (PHP) stackoverflow.com/questions/8485886/…, you should use Content-Disposition: attachment Commented Apr 9, 2015 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. Commented Apr 9, 2015 at 18:59
  • @mason, WinForm application. It works good without the WinForm application. Commented Apr 9, 2015 at 19:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The document is sadly just hardcoded text (not an ASP.NET page), and it will be too high of a risk to change this now. So I'm guessing option 1 won't be very easy, since the page is created by documentContent.Text = "<table cellpadding ....."> etc. The page is created by adding <a href>, no buttons. Any suggestions without using buttons? Or am I able adding a button within documentContent.Text = <some code>? Thanks!
Can you change the HTML and just remove the call to open the new window? If you can't change the HTML, you're probably stuck...there may be ways to override this using jQuery or something, but I wouldn't rely on something like that.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.