2011-03-28 22 views
1

在我的mvc应用程序中,我使用jQuery后使用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文件的虚拟路径的操作中。我怎样才能使下载提示用户到我的 动作如下所示

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

,而不是做这个AJAX查询,这似乎是不必要的,你可以写:

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

事实上文件。无论如何,浏览器都会提示进行操作(保存或打开)。无需添加另一个提示。 – 2011-03-28 13:26:42

+0

另请注意,Kon的使用DownloadResult类的答案对所有未来的下载操作都很好:http://stackoverflow.com/questions/5459451/download-prompt-from-mvc/5459527#5459527 – hunter 2011-03-28 13:26:57

+0

@hunter ..我不能当然,如果路径可用或不。并且我必须使用jQuery的Ajax由于某种原因。 .Thanx – 2011-03-28 13:53:27