How to make Safari and IE download image instead of opening in a new page?

后端 未结 4 1644
庸人自扰
庸人自扰 2020-12-31 02:52

In Firefox and Chrome this link property \"download=img.jpg\" works fine and shows the download window instead of a new tab or page.



        
4条回答
  •  孤城傲影
    2020-12-31 03:21

    This is a way to do it in IE with JavaScript. However, I can't find a solution for Safari yet

    var canvas = document.createElement('canvas');
    var img = document.createElement('img');
    img.onload = function(e) {
        canvas.width = img.width;
        canvas.height = img.height;
        var context = canvas.getContext('2d');
        context.drawImage( img, 0, 0, img.width, img.height);
        window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg');
    }
    img.src = 'img.jpg;
    

提交回复
热议问题