使用reactJS从http响应下载文件

时间:2018-12-28 07:37:40

标签: javascript ajax reactjs google-chrome extjs

我有这样的代码:

getZipFile(){
  Ext.Ajax.request({
    url: "http://localhost:8081/myAppName/protected/upload/download.do",
    method: 'POST',
    params: {
        entityId: this.props.currentEntityId
    },
    jsonData: litOfDocumentsIdINeed,
    headers: {
      'Content-Type': 'application/json'
    },
    callback: function (records, operation, success) {
      //
      //  I have what I need at this point, it is inside of 'records'
      //  the problem is here: I get the document in records, but I need
      //  to Download it automatically, as soon as I receive these records
      //  But HOW?
      //
    }
  })
}

那么,我收到的文件很好,但是如何进一步执行并自动下载呢?

1 个答案:

答案 0 :(得分:0)

获得回调响应后,您可以执行以下操作-

var filename = 'test' //set the file name here
var blob = new Blob([records], { type: 'add file type here' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;

document.body.appendChild(link);

link.click();

document.body.removeChild(link);