用C#下载PDF文件的代码
我在下载pdf文件时遇到问题。 而其他文件则下载。 码:
WebClient client = new WebClient(); client.DownloadFile(remoteFilename, localFilename);
如果你知道,请帮助我
检查这个方法,希望有所帮助
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 } }
请尝试以下代码示例下载.pdf文件。
Response.ContentType = "Application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf")); Response.End();
@Syed Mudhasir:它只是一行:)
client.DownloadFileAsync(new Uri(remoteFilename, UriKind.Absolute), localFilename);
它将下载pdf文件。 🙂
这对我有用:
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();