简体   繁体   English

使用reactJS从http响应下载文件

[英]Download file from http response, using reactJS

I have a code like this: 我有这样的代码:

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?
      //
    }
  })
}

So, I receive that file, that's good, but how to go step further and to automatically download it? 因此,我收到了该文件,很好,但是如何进一步执行并自动下载呢?

You can do something like this after getting the callback response - 得到回调响应后,您可以执行以下操作-

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM