tkc*_*kcn 6 c# ajax get extjs download
我希望客户端在单击按钮时下载存储在我的数据库中的文件.我发送这个ajax请求并从服务器端获取它.
EXTJS:
downloadFile: function (a, b, c) {
var feed_id =this.getMyfeedwindow().down('form').getComponent('FeedId').text;
Ext.Ajax.request({
url: '/Feed/Download',
method: 'GET',
params: {
fileID: feed_id, //this.form.getComponent('file').value,
},
failure: function (response) {
alert('failed !');
},
success: function (response) {
alert('success!');
},
});
},
Run Code Online (Sandbox Code Playgroud)
然后用这个代码块满足请求.
C#:
public void Download(string fileID){
Response.ContentType = "application/force-download";
Response.AddHeader("Content-Disposition", "attachment; Filename=\"Logo1.jpg\"");
Response.BinaryWrite(data);
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
当我用firebug检查网络时,似乎我的请求使用这些参数成功返回.
Cache-Control private
Content-Disposition attachment; filename="Logo1.jpg"
Content-Type application/force-download
Date Wed, 09 Jan 2013 12:51:54 GMT
Server Microsoft-IIS/8.0
Transfer-Encoding chunked
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 4.0
X-Powered-By ASP.NET
X-SourceFiles =?UTF-8?B?RTpcVXRrdUNhblxQcm9qZWN0c1xURlNcQlRPTVxCVE9NXEZlZWRcRG93bmxvYWQ=?=
Run Code Online (Sandbox Code Playgroud)
虽然它返回成功,但下载无法启动.我阅读了很多问题和文章,但大多数答案都说添加force-download标题解决了这个问题.我想念哪一点?谢谢.
要处理下载,您应该使用提供的帮助程序之一
大多数时候我都在使用System.Web.MVC.FileStreamResult
自己。使用它就像
FileStreamResult result = new FileStreamResult(stream, contentType);
result.FileDownloadName = filename; // name of the downloaded file
Run Code Online (Sandbox Code Playgroud)
根据您的编辑仅更新一些信息
您无法使用 XHR 请求开始下载。但至少有两种方法可以做到这一点:
top.location.href = "YourPath";
在 ajax 调用的成功处理程序中设置。[有关top.location.href的信息]