AngularJs dynamic download from response

前端 未结 1 390
北恋
北恋 2021-01-13 11:19

I have written a directive based on Scott\'s answer. You\'d use it like so:

相关标签:
1条回答
  • 2021-01-13 12:22

    Problem:

    I have resolved this issue. It appears that Internet Explorer 11 does not like aliases to the msSaveBlob function, demonstrated by the simplest examples:

    // Succeeds
    navigator.msSaveBlob(new Blob(["Hello World"]), "test.txt");
    
    // Fails with "Invalid calling object"
    var saveBlob = navigator.msSaveBlob;
    saveBlob(new Blob(["Hello World"]), "test.txt");
    

    So essentially the generic alias created to encapsulate saveBlob functionality, which should be permissible, prevents msSaveBlob from working as expected:

    var saveBlob = navigator.msSaveBlob || navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
    

    Workaround:

    So the work around is to test for msSaveBlob separately.

    if(navigator.msSaveBlob)
        navigator.msSaveBlob(blob, filename);
    else {
        // Try using other saveBlob implementations, if available
        var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
        if(saveBlob === undefined) throw "Not supported";
        saveBlob(blob, filename);
    }
    

    tl;dr

    So the msSaveBlob method works, as long as you don't alias the function. Perhaps it's a security precaution - but then perhaps a security exception would have been more appropriate, though I think it is most likely a flaw, considering the source. :)

    0 讨论(0)
提交回复
热议问题