首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExtJS 5-从POST servlet请求下载文件

ExtJS 5-从POST servlet请求下载文件
EN

Stack Overflow用户
提问于 2015-03-12 14:47:01
回答 3查看 6.9K关注 0票数 1

我尝试使用表单提交方法在ExtJS 5中实现导出功能。我看了下面的堆栈溢出链接,它有帮助,但不完全。

Extjs 4 (with a code for 3.4 below) downloading a file returned from a post request

在我的示例中,在请求响应成功后,我遇到了一个问题,获得了无效的JSON编码error.Even,我试图将读取器从JSON读取器更改为其他字符串读取器(在链接中提到),但由于某种原因,它是相当成功的。

http://www.sencha.com/forum/showthread.php?86704-handling-xml-response-on-form-submit

代码:-

代码语言:javascript
复制
  var form = Ext.create('Ext.form.Panel',{
    timeout: 60000
  });
  var basicForm = form.getForm();
  basicForm.errorReader= new String();
    basicForm.submit({
        url     :  GRID_EXPORT_URL,
        method  : 'POST',
        headers : {
            "USER": user,
            "SERVERSESSIONID": serverSessionId,
            "Content-Type":"application/x-www-form-urlencoded"
        },  
        params  : {
            gridId:"dummyGrid",
            colDescs:"col1,Name"
        },              
        scope   : this,
        success : function(responseText){              
        },
        target: '_blank' 
    });  

错误消息:-

代码语言:javascript
复制
   [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: Code

来自Java的输出响应:-

代码语言:javascript
复制
    Id,Name
    13092,Thiru
    12767,Arasu
    117,Vinod

我认为由于这个编码问题,即使在请求返回200成功状态之后,浏览器下载窗口也不会弹出!您的帮助是非常感谢的,谢谢提前!

我已经修改了代码,如下所示,但是浏览器下载仍然不会发生事件,尽管响应是200。

带有Iframe/Form的修改代码:-

代码语言:javascript
复制
onClickExport : function(){
    var body = Ext.getBody();
    var downloadFrame = body.createChild({
         tag: 'iframe',
         cls: 'x-hidden',
         id: 'app-upload-frame',
         name: 'uploadframe'
     });      
    var downloadForm = body.createChild({
         tag: 'form',
         cls: 'x-hidden',
         id: 'app-upload-form',
         target: 'app-upload-frame'
     });        
    Ext.Ajax.request ({
      url     : EXPORT_URL,
      method  : 'POST',
      form    : downloadForm,       
      timeout : 30 * 60 * 1000, // 30 Minutes should be OK.
      scope   : this,
      headers : {
            "USER": user,
            "SERVERSESSIONID": serverSessionId,
            "Content-Type":"application/x-www-form-urlencoded"
      },  
      params  : {
            gridId:"dummyGrid",
            colDescs:"col1,Name"
      }, 
      success : function (r) {
        alert("Success");
      },
      failure: function(r) {
        alert('error');
      }
    }); 

注意:我正在使用谷歌Chrome浏览器!

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-02 13:50:49

导出可以通过ajax调用实现,从响应中创建一个Blob,并使用msSaveBlob进行保存。此功能适用于现代浏览器ie10及以上版本。

代码语言:javascript
复制
onClickExport: function() {
    CUIF.Ajax.request({
        url: '......',
        method: 'POST',
        scope: this,
        params: {
           ...
           ...
         },
        success: function(data,response) {
            this.onExportSuccess(response);
        }
    });
},

onExportSuccess: function(response){
     this.getView().unmask("Loading...");
     var disposition = response.getResponseHeader('Content-Disposition');
     var filename = disposition.slice(disposition.indexOf("=")+1,disposition.length);
     var type = response.getResponseHeader('Content-Type');
     var blob = new Blob([response.responseText], { type: type });
     if (typeof window.navigator.msSaveBlob !== 'undefined') {
        // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created These URLs will no longer resolve as the data backing the URL has been freed."
        window.navigator.msSaveBlob(blob, filename);
     } 
     else {
        var URL = window.URL || window.webkitURL;
        var downloadUrl = URL.createObjectURL(blob);
        if (filename) {
            // use HTML5 a[download] attribute to specify filename
            var a = document.createElement("a");
            // safari doesn't support this yet
            a.href = downloadUrl;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
        } 
        setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
    }    
}
票数 4
EN

Stack Overflow用户

发布于 2015-03-12 21:26:09

您对响应有任何头配置吗?

代码语言:javascript
复制
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
票数 0
EN

Stack Overflow用户

发布于 2018-05-02 18:51:06

使用不可见的表单和0高度的iframe创建像这样的Sencha OnPostDownloader组件。

代码语言:javascript
复制
Ext.define('MyApp.view.OnPostDownload', {
  extend: 'Ext.Component',
  alias: 'widget.OnPostDownloader',
  autoEl: {
    tag: 'iframe',
    cls: 'x-hidden',
    src: Ext.SSL_SECURE_URL
  },
  postAndDownload: function (config) {
    var invsibleForm = Ext.create('Ext.form.Panel', {
      title: 'invsibleForm',
      standardSubmit: true,
      url: config.url,
      timeout: 120000,
      height: 0,
      width: 0,
      hidden: true,
      items: [ {
        xtype: 'hiddenfield',
        name: 'mydata',
        value: config.params
      } ]
    });

    invsibleForm.getForm().submit();
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29012888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档