用C#下载PDF文件的代码

时间:2011-01-10 07:17:22

标签: c# .net

我在下载pdf文件时遇到问题。而其他文件则下载。 代码:

WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
如果你知道的话,请帮助我

7 个答案:

答案 0 :(得分:8)

检查这个方法,希望有所帮助

        public static void DownloadFile(HttpResponse response,string fileRelativePath)
    {
        try
        {
            string contentType = "";
            //Get the physical path to the file.
            string FilePath = HttpContext.Current.Server.MapPath(fileRelativePath);

            string fileExt = Path.GetExtension(fileRelativePath).Split('.')[1].ToLower();

            if (fileExt == "pdf")
            {
                //Set the appropriate ContentType.
                contentType = "Application/pdf";
            }

            //Set the appropriate ContentType.
            response.ContentType = contentType;
            response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(fileRelativePath)).Name);

            //Write the file directly to the HTTP content output stream.
            response.WriteFile(FilePath);
            response.End();
        }
        catch
        {
           //To Do
        }
    }

答案 1 :(得分:3)

请尝试使用以下代码示例下载.pdf文件。

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

答案 2 :(得分:1)

@Syed Mudhasir: 它只是一行:)

client.DownloadFileAsync(new Uri(remoteFilename, UriKind.Absolute), localFilename);

它将下载pdf文件。 :)

答案 3 :(得分:0)

这些解决方案都不适合我,或者还不完整。也许这篇文章是旧的,而.NET的较新版本使它更容易了?无论如何,下面的小代码对我的工作是如此出色:

  static async Task DownloadFile(string url, string filePath)
  {
     using (var wc = new WebClient())
        await wc.DownloadFileTaskAsync(url, filePath);
  }

以及如何调用该方法:

Task.Run(async () => { await DownloadFile(url, filePath); }).Wait();

答案 4 :(得分:0)

    public void DownloadPdf(String downloadLink, String storageLink)
    {
        using (WebClient wc = new WebClient())
        {
            wc.DownloadFile(downloadLink, storageLink);
        }
        Console.Write("Done!");
    }

    static void Main(string[] args)
    {
        Program test = new Program();
        test.DownloadPdf("*PDF file url here*", @"*save location here/filename.pdf*");
        Console.ReadLine();
    }

确保添加:使用System.Net;

答案 5 :(得分:-1)

public void DownloadTeamPhoto(string fileName)
{
  string mimeType = MimeAssistance.GetMimeFromRegistry(fileName);
  Response.ContentType = mimeType;
  Response.AppendHeader("Content-Disposition", "attachment; filename=" + 
  Path.GetFileName(basePath + fileName)); //basePath= @"~/Content/
  Response.WriteFile(basePath + fileName); //basePath= @"~/Content/
  Response.End();

}

public static string GetMimeFromRegistry(string Filename)
{
  string mime = "application/octetstream";
  string ext = System.IO.Path.GetExtension(Filename).ToLower();
  Microsoft.Win32.RegistryKey rk = 
  Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);

  if (rk != null && rk.GetValue("Content Type") != null)
     mime = rk.GetValue("Content Type").ToString();

  return mime;
}

答案 6 :(得分:-2)

这对我有用:

string strURL = @"http://192.168.1.xxx/" + sDocumento;
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + sDocumento + "\"");
byte[] data = req.DownloadData(strURL);
response.BinaryWrite(data);
response.End();