我在下載pdf文件時遇到問題。而下載其他文件。 代碼:代碼在C#中下載PDF文件
WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
請幫助我,如果你知道
我在下載pdf文件時遇到問題。而下載其他文件。 代碼:代碼在C#中下載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
}
}
@Syed Mudhasir: 它只是一個行:)
client.DownloadFileAsync(new Uri(remoteFilename, UriKind.Absolute), localFilename);
它將下載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();
這爲我工作:
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();
問題是什麼? – 2011-01-10 07:21:33
我的問題是在下載PDF文件並將其保存爲上述代碼的PDF文檔之後。文我試圖打開它顯示文件修復錯誤。 – 2011-01-10 11:11:22