How to download a base64-encoded image?

前端 未结 10 906
无人及你
无人及你 2020-11-29 08:16

I have a base64-encoded image from the server for which I want to force the download through JavaScript. Is is possible?

10条回答
  •  独厮守ぢ
    2020-11-29 08:39

    If you want to download it using JavaScript (without any back-end) use:

    window.location.href = 'data:application/octet-stream;base64,' + img;
    

    where img is your base64 encoded image.

    If you want to allow the user to specify a file name, use the download attribute of the a tag:

    Download
    
    • Notice: The download attribute is not widely supported.

提交回复
热议问题