Webclient.DownloadFile() .aspx file won't open
Asked Answered
P

1

3

I'm using powershell for the first time. I've learned how to download files using a webclient, using the following code.

$client = New-Object System.NET.Webclient
$client.DownloadFile( $url, $path )

This seems to work nicely for what I am trying to eventually do, which is download multiple files from a web page at one time. I tried this on a site, which has its files formatted as .pfva files, which are opened as a PDF. no problem. This was a password protected site too.

So moving to the site that I actually want to use it on. Again, a site requiring a log in, although I just sign in on my browser and then run the webclient. Probably why I never have to pass authentication in the command script....

This site's files are formatted as .aspx files. They are to be opened as PDF files. I can simply click on the file, save or open, and it works naturally as a PDF file. But when I use the webclient.download file, it downloads to the right spot...but I get an error when trying to open it.

"Adobe can't open file. It was not coded properly" ...something along those lines. I can't get the message up now because I'm at work. The URL for the downloads are in the following format.....

https://www.WebsiteABC.com/ShowDocument.aspx?DocPath=%7e%5cDocument%5cb75c6093-697a-4e59-bc26-fa2eb24f57f7%5cAUTHORIZATION.PDF

Why won't it open!?! Is there a way around this. Any help would be appreciated. Thank you!.

OH btw, I set the $path to a .PDF file in a directory on my computer...because I read that a path to a file should be provided, and not just a directory.

Peregrination answered 19/10, 2012 at 15:28 Comment(2)
Was your file actually downloaded, or is it 0 bytes? Can you compare it to the file you have downloaded manually?Barrybarrymore
Also you should check the contents of the downloaded file, it might be a webpage with code to redirect to the actual PDFMiscellanea
M
7

Looks like your script works in the most of cases.

I found this page, telling that user agent HTTP header field can be important for server, so try this one:

$userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
$url = "https://www.adobe.com/enterprise/pdfs/pdfarchiving.pdf"
$path = "c:\test.pdf"

$client = New-Object System.NET.Webclient
$client.Headers.Add("user-agent", $userAgent)
$client.DownloadFile( $url, $path )
Minor answered 28/2, 2013 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.