2011-03-28 4 views
1

내 mvc 응용 프로그램에서 jquery를 사용하여 아약스 게시물을 사용합니다. 아래에 예가 나와 있습니다.MVC에서 다운로드 프롬프트

var postData = { recordID: selectedRecord }; 
      $.ajax({ 
       type: "POST", 
       data: postData, 
       url: '<%= Url.Action("LoadAudioPlayer", "Search") %>', 
       success: function (result) { alert(result); }, 
       error: function (result) { alert('error'); } 
      }); 

.wav 파일의 가상 경로를 만드는 동작. 내가 경고 .I를 넣어 원인 그는 파일을 다운로드 할 말든 내가 사용자에게 프롬프트를 만들고 싶어 경로를 볼 수 있습니다 내 작업이

public string LoadAudioPlayer(string recordID,SearchModels searchModel) 
    { 
     //doing some operations 
     path=getPath(); \\getting virtual path of the file 

     return path; 
    } 

아래에 표시되어 사용자 로 다운로드 프롬프트를 만들 수있는 방법 . jquery로 어떻게 처리 할 수 ​​있습니까?

답변

2

경로와 함께 새 창 열기 (팝업)를 성공 함수에 추가해야합니다. 이렇게하면 클라이언트 브라우저에서 서버에 저장된 파일을 쿼리하고 클라이언트에게 다운로드할지 여부를 묻습니다.

var postData = { recordID: selectedRecord }; 
      $.ajax({ 
       type: "POST", 
       data: postData, 
       url: '<%= Url.Action("LoadAudioPlayer", "Search") %>', 
       success: function (result) { window.open(result,'',''); }, 
       error: function (result) { alert('error'); } 
      }); 
2

제가 생각하기에, this article은 당신이하고 싶은 것과 비슷한 것을 설명합니다.

2

오히려 불필요한 것이 아약스 쿼리를하는 것보다, 당신은 쓸 수 :

window.location = '<%= Url.Action("LoadAudioPlayer", "Search") %>/' + recordID; 

와 액션 메소드는 실제로

public string LoadAudioPlayer(string recordID, SearchModels searchModel) 
{ 
    //doing some operations 
    var path = getPath(); //getting virtual path of the file 

    if (System.IO.File.Exists(path)) 
    { 
     var fileName=getFileName(); 

     ControllerContext.HttpContext.Response.AddHeader("content-disposition", 
      "attachment; filename=" + fileName) 

     return File(path, contentType); 
    } 

    return new EmptyResult(); 
} 
+0

존재하지 않는 파일을 처리하기 위해이 변경 될 수 있습니다. 브라우저는 어쨌든 조치 (저장 또는 열림)를 요구합니다. 아직 다른 프롬프트를 추가 할 필요가 없습니다. –

+0

또한 Kon의 대답은 DownloadResult 클래스를 사용하여 향후 모든 다운로드 작업에 유용 할 것이라는 점에 유의하십시오. http://stackoverflow.com/questions/5459451/download-prompt-from-mvc/5459527#5459527 – hunter

+0

@ 헌터 .. 경로가 사용 가능한지 확인하십시오. 그리고 이유가 있기 때문에 아약스에서 jquery를 사용해야합니다. .Thanx –