Interface to get the binary file, use the a tag to download, do not know the file name. How to parse to get the file name.

The

interface gets the binary file and downloads it using the a tag. The file name is not known. How to parse to get the file name.

  var blob = this.response;
  var reader = new FileReader();
        reader.readAsDataURL(blob); // base64ahref
        reader.onload = function (e) {
            // a
            var a = document.createElement("a");
            a.download = fileName;
            a.href = e.target.result;
            $("body").append(a); // firefoxclick
            a.click();
            $(a).remove();
            defer.resolve(true);
        };

how to make the downloaded file get its own file name automatically without fileName.
this method does not seem to work to download files of 5 and 6mb, and does not jump out of the file selection box.
do you have any good ideas?

using fileSave, it seems that you also need a file name to save.

Feb.14,2022

get the file name according to 'Content-Disposition' through the back-end coordination.

  
  
        var xhr = new window.XMLHttpRequest();
        xhr.open('GET', url, true); // POST
        xhr.responseType = 'blob'; // blob
        // /
        xhr.onload = function () {
            // 
            if (this.status === 200) {
                // 200
                var content = this.response;
               
                if (!fileName) fileName = new Date().format('yyyyMMdd');
                var oldFileName = fileNameFromHeader(xhr.getResponseHeader('Content-Disposition'));
                if (oldFileName != null) {
                    if (fileName.lastIndexOf('.') <= 1) {
                        fileName = fileName + oldFileName.substring(oldFileName.lastIndexOf('.'));
                    } else {
                        fileName = fileName.substring(0, fileName.lastIndexOf('.')) + oldFileName.substring(oldFileName.lastIndexOf('.'));
                    }
                }
                try {
                    saveAs(content, fileName);
                } catch (error) {
                   
            } 
        };
        
         // 
    function fileNameFromHeader (disposition) {
        var result = null;
        if (disposition && /filename=.*/ig.test(disposition)) {
            result = disposition.match(/filename=.*/ig);
            return decodeURI(result[0].split('=')[1]);
        }
        return null;
    }
Menu