Code for loading a PDF file in C #

I have a problem downloading PDF files. while other files load. code:

WebClient client = new WebClient(); client.DownloadFile(remoteFilename, localFilename); 

Help me if you know

+4
source share
4 answers

check this method, hope it helps

  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 } } 
+8
source

Please try the following code sample to download the .pdf file.

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

@Syed Mudhasir: Its just a string :)

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

It will download pdf files. :)

+1
source

This worked for me:

 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(); 
-1
source

Source: https://habr.com/ru/post/1334888/


All Articles