Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a windows application in vb.net in which i have a url which first ask me to login on the website and then display a view pdf link. As i make it click it again redirect to another page where instead of asking for download pdf it opens it in my web browser control. Now i want to save that opened pdf on my specified path. I have googled a lot but didn't find any solution for the same. I even found some related posts but none of them have my answer. Here my pdf url doesnt contains any file name like '.pdf'. Url contains some token values. To open this url it requires login on the website. I am trying to download pdf file for many days. Please help me.
Posted

The behaviour of the browser is based on the response type set in the response header.

This is called the MIME type and each type of file has one.

If you set the response type to application/pdf then it will instruct the browser to treat the file as a pdf file.

You can also set the file name using:

C#
this.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);


Unfortunately, the server can't control how the browser decides to handle that file type. If you're using a browser with a PDF viewer add in. The browsers response could be to launch the viewer instead of simply downloading the file.

But the default Adobe PDF view should let you save the file.
 
Share this answer
 
v2
Comments
rohitvermasrt 12-Aug-13 9:02am    
Thanks for the reply but in webbrowser control of windows applicaiton, how can i set response header. I think there is nothing where i can set this for webbrowser control.
Stephen Hewison 12-Aug-13 9:21am    
It's not set by the web browser. It's set by the web server. Are you not in control of the server supplying the pdfs?

If you want to do it completely from the client. You could use the WebRequest object to get the file bytes and handle the save in the client instead of using a web browser control.
rohitvermasrt 12-Aug-13 9:48am    
Hi stephen,
No web server is not under my control. Actually i am developing a scrapping application. And the pdf i am getting is not having any static url. I have already tried web request object but no success. The final url i get for downloading pdf is also redirects internally 2-3 time. So in httpwebrequest i can not find any pdf. I have also tried Webclient but no succes with that also.
Stephen Hewison 13-Aug-13 3:39am    
You need to interrogate the WebResponse object returned by the WebRequest as this will have the relevant HTTP redirect codes and the redirect address. You'll be able to follow this until you find the PDF.
rohitvermasrt 19-Aug-13 3:29am    
Hi Stephen,
I tried everything possible from my side regarding downloading the pdf file from the url but no success.
VB
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
   (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
   ByVal lpfnCB As Long) As Long



Use in your downloading code :

VB
URLDownloadToFile(0&, strURL, strPath, 0&, 0&)

       MsgBox("Download finished - downloaded " & strURL & " to " & strPath)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900