加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Extjs Ajax文件下载请求C#MVC

发布时间:2020-12-15 22:56:05 所属栏目:百科 来源:网络整理
导读:我希望客户端在单击按钮时下载存储在我的数据库中的文件. 我发送这个ajax请求并从服务器端获取它. EXTJS: downloadFile: function (a,b,c) { var feed_id =this.getMyfeedwindow().down('form').getComponent('FeedId').text; Ext.Ajax.request({ url: '/Fe
我希望客户端在单击按钮时下载存储在我的数据库中的文件.
我发送这个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!');
        },});
},

然后用这个代码块满足请求.

C#:

public void Download(string fileID){
    Response.ContentType = "application/force-download";
    Response.AddHeader("Content-Disposition","attachment; Filename="Logo1.jpg"");
    Response.BinaryWrite(data);
    Response.End();
 }

当我用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=?=

虽然它返回成功,但下载无法启动.我阅读了很多问题和文章,但大多数答案都说添加force-download标题解决了这个问题.我想念哪一点?谢谢.

解决方法

要处理下载,您应该使用提供的帮助程序之一

> System.Web.MVC.FilePathResult
> System.Web.MVC.FileStreamResult
> System.Web.MVC.FileContentResult

大多数时候我使用的是System.Web.MVC.FileStreamResult.像它一样使用它

FileStreamResult result = new FileStreamResult(stream,contentType);
result.FileDownloadName = filename; // name of the downloaded file

根据您的编辑更新一些信息

您无法使用XHR请求开始下载.但至少有两种方法可以做到:

>如果文件路径已修复,并且您知道它设置了top.location.href =“YourPath”;在ajax调用的成功处理程序中. [信息约top.location.href]>如果你动态创建文件并想要返回它,你应该创建一个隐藏的iframe并在其中注入一个表单然后执行请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读