2011-01-10 5 views
4

pdf 파일을 다운로드하는 데 문제가 있습니다. 다른 파일은 다운로드됩니다. 코드 : 유C#에서 PDF 파일을 다운로드하는 코드

+7

문제가 무엇입니까? –

+0

내 문제는 위에서 언급 한 코드 대신 pdf 파일을 다운로드하여 pdf 문서로 저장 한 후입니다. wen 열려고하면 문서 복구 오류가 표시됩니다. –

+0

내가 ... 링크 '당신을 도울 것이다 아래이 희망 http://stackoverflow.com/questions/10829168/can-we-use-response-flush-instead-of-response-end/17038408 # 17038408 – Viishnuu

답변

8

확인이 방법을 알고있는 경우

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

pls는 저를 도와, 희망

 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

contentType = "Application/pdf"를 설정해야합니다. 다운로드 한 파일이 올바른지 확인하십시오 PDF 파일 –

+0

감사합니다 Mahmoud 시도했지만 다음 오류가 발생합니다 : 형식 또는 네임 스페이스 이름 'HttpResponse'찾을 수 없습니다 (사용 지시문 또는 어셈블리 참조가 누락되었습니다.) –

+0

있습니다. 당신은 웹 응용 프로그램이나 데스크탑 응용 프로그램에서 일하고 있습니까 ?? –

1

@Syed Mudhasir하는 데 도움이 : 그것의 단지 라인 :

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

PDF 파일을 다운로드합니다. :)

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

이 나를 위해 일한 :

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(); 
관련 문제