ExtJS的4(用code为3.4以下)下载文件从POST请求返回文件、code、ExtJS、POST

2023-09-10 16:29:26 作者:隐疾

我已经看到稍微与此相关的问题,但没有一个回答我的问题。我设置了一个Ext.Ajax.request如下:

I have seen questions slightly related to this, but none that answer my problem. I have set up an Ext.Ajax.request as follows:

var paramsStringVar = 'param1=1&param2=two&param3=something&param4=etc';

Ext.Ajax.request({
  url: '/cgi-bin/url.pl',
  method:'POST',
  params:paramsStringVar,
  timeout:120000,
  success: function(response, opts){
    var objhtml = response.responseText; //content returned from server side
    console.log(objhtml);
  }

});

此请求检索来自后端相应的内容。一个参数是输出类型,这可能需要值{HTML时,Excel,CSV}。当返回的HTML显示我能够处理并正确显示。现在就到了问题...

This request retrieves the appropriate content from the backend. One parameter is outputType, which can take values {html, excel, csv}. When returning html to display I am able to handle and display it correctly. Now on to the problem...

当我设置的输出类型参数为CSV或excel,我回来了合适的内容为CSV或要求TSV(EXCEL)。但是,我不想要的内容,我希望有一个提示下载文件(CSV或excel)。我怎样才能让浏览器自动提示只是检索的ExtJS中的文本内容的下载文件的用户呢?

When I set the outputType parameter to csv or excel, I get back the appropriate content as csv or tsv(excel) as requested. BUT, I don't want the content, I want a prompt to download the file(csv or excel). How can I have the browser auto prompt the user to download the file instead of just retrieving the text content within extjs?

版本4.07,所以我不能使用任何4.1独有特性

Version 4.07 so I can't use any 4.1 only features

推荐答案

下面是我的解决办法。这是我有它目前正在努力。该反应生成下载/打开的提示符,基于文本/ CSV的响应类型。需要注意的是没有的iFrame或引用的iframe是必要的。我花了很多时间就需要一个iFrame,这实际上打破了我的解决方案,挂断了电话。一个IFRAME并不需要产生一个下载提示。我们需要的是一个请求(提交)与此类似,还有一个后台生成相应的CSV文本/ CSV响应头。

Below is my solution. This is how I have it currently working. The response generates a download/open prompt, based on a response type of text/csv. Note that no iFrame or reference to an iframe are needed. I spent a lot of time hung up on the need for an iFrame, which actually broke my solution. An iFrame is not needed to generate a download prompt. What is needed is a request(submittal) similar to this one, along with a backend generating the appropriate csv with text/csv response header.

var hiddenForm = Ext.create('Ext.form.Panel', {
  title:'hiddenForm',
  standardSubmit: true,
  url: /cgi-bin/url.pl
  timeout: 120000,
  height:0,
  width: 0,
  hidden:true,
  items:[
    {xtype:'hiddenField', name:'field1', value:'field1Value'},
    // additional fields
  ]
})

hiddenForm.getForm().submit()

该standardSubmit行是至关重要的。

The standardSubmit line is vital