以编程方式下载pdf
问题描述:
如何使用vb.NET或C#下载PDF并将其存储到磁盘?
How can I download a PDF and store to disk using vb.NET or C#?
在到达最终PDF之前,(PDF的)URL进行了一些重新设计.
The URL (of the PDF) has some rediection going on before the final PDF is reached.
我尝试了以下操作,但是当我尝试在本地打开时,PDF似乎已损坏
I tried the below but the PDF seems corrupted when I attempt to open locally,
Dim PdfFile As FileStream = File.OpenWrite(saveTo)
Dim PdfStream As MemoryStream = GetFileStream(pdfURL)
PdfStream.WriteTo(PdfFile)
PdfStream.Flush()
PdfStream.Close()
PdfFile.Flush()
PdfFile.Close()
答
您可以尝试使用WebClient(System.Net命名空间)类来执行此操作,这将避免任何流工作.
You can try to use the WebClient (System.Net namespace) class to do this which will avoid any stream work on your side.
以下C#代码获取IRS表单并将其保存到C:\ Temp.pdf.
The following C# code grabs an IRS form and saves it to C:\Temp.pdf.
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", @"C:\Temp.pdf");
}