0

I have a dynamically generated PDF that I wish to download to the harddrive. The file download is started by the vb.net code:

WebBrowser1.Navigate("javascript:fnSubmitPDFForm('modalDivpdf')")

and I currently get the normal internet explorer 'File Download' window with the Open Save Cancel options. I would like to specifiy a folder and filename and automatically download the PDF. Is this possible?

Here is a better example of a link that induces the 'File Download' window. I'd like to automatically save the PDF:

WebBrowser1.Navigate("http://www2.hungryhorse.co.uk/index.php/download_file/view/838/71/")

Thanks

user1228123
  • 424
  • 4
  • 15

2 Answers2

0

You can neither automatically download the file nor specify a path. You can however give a suggested filename:

context.Response.ContentType = "application/octet-stream"                                                         
context.Response.AddHeader("content-disposition", "attachment; filename=""" & "yourFilename.pdf & """")
context.Response.TransmitFile(actualFile)

Where yourFilename.pdf is the suggested filename and actualFile is the full path of the file to send.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Shame there is no way of doing it via WebBrowser. Could I do it using HTTPWebRequest or WebClient? – user1228123 Oct 06 '12 at 22:11
  • Oh, hang on, I mis-read your question as if you were using a browser as the client, not a WebBrowser control hosted in a Windows Form. It looks like you need to intercept HTTP traffic, in which case Eric J.'s reply might be useful to you: http://stackoverflow.com/questions/11075966/vb-net-intercept-http-webbrowser-traffic But yes, if you can skip the use of a WebBrowser control than could be cleaner. – Andrew Morton Oct 06 '12 at 22:21
0

I know this is an old thread, but I found it yesterday trying to solve the same problem. May sources say that it is not possible to do this, but it IS possible with a workaround. There are many reasons why this is needed; some wisecracks in other forums resort to "but why do you need it".

I must add that the solution below is not the most elegant, but perhaps it is the simplest, and it allowed me to solve the problem in a couple of hours.

There are two steps:

  1. This will cause links to PDFs to automatically trigger the dialog asking if you want to save the file (with a default to "Cancel"). I did it by installing and then removing Adobe Reader. I know that there must be an elegant way around it by looking at some key in the registry, or by configuring the web browser, but I run out of time with this problem.

  2. Send key strokes to the pop-up dialogs.

The code looks like:

Public Sub YourFunction()
    '...
    WebBrowser1.Navigate("https://... your URL")
    Wait(2)
    ' Accept question to save
    SendKeys.SendWait("{LEFT}")
    Wait(0.5)
    SendKeys.SendWait("{ENTER}")
    Wait(0.5)
    ' Actually save the file 
    SendKeys.SendWait("{ENTER}")
    Wait(0.5)
    '...
End Sub

The function "Wait" is needed to allow time to the WebBrowser control to actually navigate to the target URL; this might be different in your case. The code is :

Public Sub Wait(ByVal seconds As Double)
   Static start As Date
   start = Now()
   Do While Now() < start.AddSeconds(seconds)
       System.Windows.Forms.Application.DoEvents()
   Loop 
End Sub

It works. I just downloaded 1,700 PDFs (for my own purpose, not going to explain it). It took a little bit of time, though.

jbgm
  • 1