[英]Download pdf programmatically
如何使用 vb.NET 或 C# 下載 PDF 並存儲到磁盤?
在到達最終 PDF 之前,(PDF 的)URL 會進行一些重定向。
我嘗試了以下操作,但是當我嘗試在本地打開時,PDF 似乎已損壞,
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 命名空間)類來執行此操作,這將避免您這邊的任何流工作。
以下 C# 代碼獲取 IRS 表單並將其保存到 C:\\Temp.pdf。
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", @"C:\Temp.pdf");
}
您也可以嘗試以下代碼示例下載pdf文件
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf");
Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf"));
Response.End();
當 URL 指向“showdocument.aspx”頁面時,您如何才能使用 client.downloadfile。
示例: https : //gaming.nv.gov/modules/showdocument.aspx?documentid=246
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.