How to deal with a request to return the binary data stream into xlsx file?

/ * FileName: file name res: a binary stream * /
function getOutExcel(fileName, res) {
    let blob = new Blob([res], { type: 'application/x-xls' });
    if (window.navigator.msSaveOrOpenBlob) {
        // Ken'yo IE & EDGE
        navigator.msSaveBlob(blob, fileName);
    } else {
        var link = document.createElement('a');
        // compatible with different browsers URL object
        const url = window.URL || window.webkitURL || window.moxURL
        // Create the download link
        link.href = url.createObjectURL(blob);
        // named Download Name
        link.download = fileName;
        // trigger click to download
        link.click();
        // the download is complete to be released
        url.revokeObjectURL(link.href);
    }
}

getOutExcel ( 'filename .xlsx', res)


Note that the type of response data added {responseType: 'blob'}
  when the interactive and background, if we are back to back binary stream of data, we shall add {responseType: 'blob'} at the time of this transmission line so returned to us is not garbled.

 

Guess you like

Origin www.cnblogs.com/changxue/p/10947384.html