0

I have this event handler that does downloading and that's working great on all browsers except IE, so I handled it with msSaveBlob and it download file, but when I open it, it said format isn't good, so my guess is that I don't use valid data in blob.

$(document).on('click', '.register-file-uploader-download-file', function () {
        let $fileResultsItem = $(this).closest('.form-fields__file-results-item');
        const fileName = $fileResultsItem.find('.multiple-files-upload-item').data('filename');
        const fileUrl = $fileResultsItem.find('.multiple-files-upload-item').val();
        if (fileUrl.length > 0) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    var url = window.URL || window.webkitURL;
                    let blobFileUrl = url.createObjectURL(this.response);
                    if (window.navigator && navigator.msSaveBlob) { // For IE
                        const blobObj = new Blob([this.response], { type: 'application/octet-stream' });
                        return navigator.msSaveBlob(blobObj, fileName);
                    }
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = blobFileUrl;
                    a.download = `${fileName}`;
                    document.body.appendChild(a);
                    a.click();
                    url.revokeObjectURL(blobFileUrl);
                }
            };
            xhr.open('GET', fileUrl);
            xhr.responseType = 'blob';
            xhr.send();
        }
    });
Serlok
  • 432
  • 1
  • 10
  • 24
  • What file format are you trying to download? Does this article help? https://github.com/angular-ui/ui-grid/issues/2312#issuecomment-66465065 I can offer a solution but it's written only with CSV files in mind. – Max Oct 09 '19 at 14:21
  • @Max This is generic control, in this situation, I am using images, but it should be implemented to use all formats – Serlok Oct 09 '19 at 14:25
  • sorry I can't be of much help. Maybe this answer is useful, if not I'm afraid I can't offer any other advice. https://stackoverflow.com/questions/24007073/open-links-made-by-createobjecturl-in-ie11 – Max Oct 09 '19 at 14:37

1 Answers1

0

I found error. Code is working fine, problem is in fact that I tried to download image that was uploaded on stage server, and when I uploaded and downloaded it and it's working

Serlok
  • 432
  • 1
  • 10
  • 24