Force download of 'data:text/plain' URL

后端 未结 5 2181
囚心锁ツ
囚心锁ツ 2020-12-01 16:31

I was wondering whether it is possible to force a browser (at least Chrome) to download a data:text/plain URL.

Chrome does download binary URLs (e.g.

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 17:37

    Try this:

    
        Download text file
    
    

    It uses HTML5 attribute download="filename.ext". (no JS needed:)

    More about: http://www.w3.org/TR/html/links.html#downloading-resources

    Browser support can be checked at http://caniuse.com/download

    (As for now, 2013, no IE nor Safari support)

    I think, you can make a fallback for not-supporting browsers: use JS to change value of href="..." to the URL of your server script (which will return the file contents with appropriate HTTP header Content-disposition: attachment;filename=filename.txt).

提交回复
热议问题