2012-08-23 7 views
8

PDF 파일을 다운로드 할 수있는 코드가있는 마스터 페이지 클라이언트 측 스크립트 (Jquery)에서 .ashx 페이지를 요청하고 있습니다. 디버깅 할 때 "파일 다운로드"코드 실행을 볼 수 있지만 파일은 다운로드되지 않습니다..ashx 페이지를 호출하여 파일 다운로드

$.ajax({ 
      type: "POST", 
      url: "FileDownload.ashx", 
      dataType: "html", 
      success: function (data) { } 
     } 
     ); 


    public class FileDownload : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     //context.Response.ContentType = "text/plain"; 
     //context.Response.Write("Hello World"); 

     string fileName = "BUSProjectCard.pdf"; 
     string filePath = context.Server.MapPath("~/Print/"); 
     context.Response.Clear(); 
     context.Response.ContentType = "application/pdf"; 
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 
     context.Response.TransmitFile(filePath + fileName); 
     context.Response.End(); 
    } 
+1

이 게시물을 도울 수 있습니까? http://stackoverflow.com/questions/1999607/download-and-open-pdf-file-using-ajax –

답변

11

귀하의 파일을 다운로드하고,하지만 당신은 Ajax를 호출하기 때문에 당신은 당신의 전화의 data 매개 변수에 자바 스크립트에 그것을 얻을.

당신은 핸들러 사용 - 자바 스크립트를 사용하여 수행하는, 그래서 여기에 필요하지 AJAX, 가장 쉬운 일이입니다 :

window.location = "FileDownload.ashx?parametres=22"; 

또는

<a target="_blank" href="FileDownload.ashx?parametres=22" >download...</a> 

아와 같은 간단한 링크,

및 URL을 통해 매개 변수를 보내면 그런 식으로 게시 할 수 없습니다.

당신은 또한 읽을 수 있습니다 : What is the best way to download file from server

+0

이것은 매우 도움이됩니다. 답에 바로 나와 있습니다. 클라이언트에서 json 호출로 .ashx에서 파일을 다운로드하는 데 몇 분이 걸리지 않으면 파일을 가져 오지 못했습니다. – amelian

+0

@Aristos 완벽하게 작동합니다. 그러나 서버 측에서 문제가 발생하면 메시지를 표시해야합니다. 어떻게해야합니까? 메시지 "잘못되었거나 파일을 찾을 수 없습니다" – Prabu

+0

@Prabu 서버 측에서는 처리기 내부에서 쉽게 오류를 기록 할 수 있습니다. 찾기 또는 쓰기 로그 클래스 ... – Aristos