Webclient.DownloadFile() .aspx file won't open

徘徊边缘 提交于 2019-12-10 20:41:15

问题


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.


回答1:


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 )


来源:https://stackoverflow.com/questions/12977549/webclient-downloadfile-aspx-file-wont-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!