Failed to execute 'atob' on 'Window'

前端 未结 4 1573
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 13:29

I\'m trying to save my HTML file in Chrome when the user presses ctrl + s keys but Chrome is crashed.

(I want to download just the source code of my HTM

4条回答
  •  伪装坚强ぢ
    2020-12-03 13:44

    BlobBuilder is obsolete, use Blob constructor instead:

    URL.createObjectURL(new Blob([/*whatever content*/] , {type:'text/plain'}));
    

    This returns a blob URL which you can then use in an anchor's href. You can also modify an anchor's download attribute to manipulate the file name:

    download me
    

    Fiddled. If I recall correctly, there are arbitrary restrictions on trusted non-user initiated downloads; thus we'll stick with a link clicking which is seen as sufficiently user-initiated :)

    Update: it's actually pretty trivial to save current document's html! Whenever our interactive link is clicked, we'll update its href with a relevant blob. After executing the click-bound event, that's the download URL that will be navigated to!

    $('#link').on('click', function(e){
      this.href = URL.createObjectURL(
        new Blob([document.documentElement.outerHTML] , {type:'text/html'})
      );
    });
    

    Fiddled again.

提交回复
热议问题