2012-07-02 34 views
1

当我使用开幕保存,另存为对话框从阿贾克斯

了window.location ='<%= Url.Action( “DownloadFile”, “家庭”)%>?FILEID = ID,

我在一个不同的屏幕上完全获取内容

但是,如何打开它作为一个弹出式的打开,保存,另存为对话框?

我打了一个ActionResult DownloadFile() 它返回一个File对象。

回答

0

您可以使用File方法的第三个参数指定文件名。这将作为一个影响到Content-Disposition HTTP响应头设置为attachment弹出另存为对话框,当用户浏览到这一行动:

public ActionResult DownloadFile(string fileId) 
{ 
    byte[] file = ... 
    // TODO: adjust the MIME type and filename extension to your case accordingly 
    return File(file, "text/plain", "foo.txt"); 
} 

然后:

var id = '1234'; 
window.location.href = '<%= Url.Action("DownloadFile", "Home") %>?fileId=' + id; 

会工作得很好。