Download pdf programmatically

狂风中的少年 提交于 2019-12-21 07:01:00

问题


How can I download a PDF and store to disk using vb.NET or C#?

The URL (of the PDF) has some rediection going on before the final PDF is reached.

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()

回答1:


You can try to use the WebClient (System.Net namespace) class to do this which will avoid any stream work on your side.

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");
}



回答2:


You can also try the following code sample to download pdf files

 Response.ContentType = "Application/pdf"; 
 Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); 
 Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf")); 
 Response.End(); 


来源:https://stackoverflow.com/questions/2913830/download-pdf-programmatically

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