Want to improve this 开发者_StackOverflowquestion? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this questionI have a problem when downloading PDF files with the following code:
WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
Whereas other files are downloaded successfully, when I download and save a PDF document it shows a document repaired error when I try to open it.
check this method , hope that 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
}
}
Please try the Following code sample to download .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();
@Syed Mudhasir: Its just a line :)
client.DownloadFileAsync(new Uri(remoteFilename, UriKind.Absolute), localFilename);
It will download pdf files. :)
None of these solutions worked for me, or were not complete. Maybe this post was old and newer versions of .NET made it easier? Anyway, the following small code did the job for me so nicely:
static async Task DownloadFile(string url, string filePath)
{
using (var wc = new WebClient())
await wc.DownloadFileTaskAsync(url, filePath);
}
And here how you can call the method:
Task.Run(async () => { await DownloadFile(url, filePath); }).Wait();
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();
}
Make sure you add: using System.Net;
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;
}
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();
精彩评论