以编程方式下载pdf [英] Download pdf programmatically
本文介绍了以编程方式下载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");
}
这篇关于以编程方式下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文