gkngkc / UnityStandaloneFileBrowser

A native file browser for unity standalone platforms
MIT License
2.01k stars 317 forks source link

WebGL IE11 download fix #36

Open dalift opened 5 years ago

dalift commented 5 years ago

Trying to use this plugin while in IE11 doesn't allow the download to happen. I'm not that good with js, but here is what I used to finally got working in any browser.

DownloadFile: function(gameObjectNamePtr, filenamePtr, byteArray, byteArraySize) {
        gameObjectName = Pointer_stringify(gameObjectNamePtr);
        filename = Pointer_stringify(filenamePtr);

        var bytes = new Uint8Array(byteArraySize);
        for (var i = 0; i < byteArraySize; i++) {
            bytes[i] = HEAPU8[byteArray + i];
        }
    if (navigator.msSaveOrOpenBlob) {
        var blob = new Blob([bytes]);

        window.navigator.msSaveOrOpenBlob(blob, filename);
        window.alert("Check bottom of screen for save options.");
        document.onmouseup = null;

            SendMessage(gameObjectName, 'OnFileDownloaded');
        return;
    }

    var downloader = window.document.createElement('a');
    downloader.setAttribute('id', gameObjectName);
    downloader.href = window.URL.createObjectURL(new Blob([bytes], { type: 'application/octet-stream' }));
    downloader.download = filename;
    document.body.appendChild(downloader);

        document.onmouseup = function() {
            downloader.click();
            document.body.removeChild(downloader);

            document.onmouseup = null;

            SendMessage(gameObjectName, 'OnFileDownloaded');
        }
    }