我在此处执行此操作:
[HttpGet]
public void Download(string FileName)
{
string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + "Exe\\";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "video/mp4";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
}
在我的一个视图中,我正在对此操作执行ajax调用,如下所示: var options = { url:'首页/下载?filename ='+ file.Name + file.Extension, 方法:'GET' };
$.ajax(options)
.done(function () {
console.log('You successfully downloaded ' + file.Name);
})
.fail(function () {
console.log('Something bad happened');
});
当我构建我的项目并导航到/ home / download?id = filename时,一切正常并且很好:chrome开始下载。
但是,当我使用上面的ajax调用来调用done
回调时,所以下载成功,但我没有看到我的浏览器(chrome)中的任何地方的下载,它不会问我在哪里放这个文件。首先,这个文件到底在哪里?为什么当我使用ajax调用时,它的行为有所不同?
感谢您的帮助。
P.S。 :从'get'变为'post'不会导致不同的结果。
答案 0 :(得分:0)
你不能用ajax来做。
Hovewer thera有很多技巧可以帮助你:
这两个是使用的方法,您也可以尝试使用现有的jquery插件。
答案 1 :(得分:0)
浏览器只会将返回的数据视为“数据”。 您只需使用隐藏的iframe
调用保存对话框即可<iframe id="#iframe" src="/home/download?id=filename"></iframe>
$('#download').click(function(){
$('#iframe').attr('src','/home/download?id=newfilename');
}