栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

浏览器/ HTML强制从src =“ data:image / jpeg; base64…”下载图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浏览器/ HTML强制从src =“ data:image / jpeg; base64…”下载图像

只需替换

image/jpeg
application/octet-stream
。客户端不会将该URL识别为可内联资源,并提示下载对话框。

一个简单的Javascript解决方案是:

//var img = reference to imagevar url = img.src.replace(/^data:image/[^;]+/, 'data:application/octet-stream');window.open(url);// Or perhaps: location.href = url;// Or even setting the location of an <iframe> element,

另一种方法是使用

blob:
URI:

var img = document.images[0];img.onclick = function() {    // atob to base64_depre the data-URI    var image_data = atob(img.src.split(',')[1]);    // Use typed arrays to convert the binary data to a Blob    var arraybuffer = new ArrayBuffer(image_data.length);    var view = new Uint8Array(arraybuffer);    for (var i=0; i<image_data.length; i++) {        view[i] = image_data.charCodeAt(i) & 0xff;    }    try {        // This is the recommended method:        var blob = new Blob([arraybuffer], {type: 'application/octet-stream'});    } catch (e) {        // The BlobBuilder API has been deprecated in favour of Blob, but older        // browsers don't know about the Blob constructor        // IE10 also supports BlobBuilder, but since the `Blob` constructor        //  also works, there's no need to add `MSBlobBuilder`.        var bb = new (window.WebKitBlobBuilder || window.MozBlobBuilder);        bb.append(arraybuffer);        var blob = bb.getBlob('application/octet-stream'); // <-- Here's the Blob    }    // Use the URL object to create a temporary URL    var url = (window.webkitURL || window.URL).createObjectURL(blob);    location.href = url; // <-- Download!};


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430294.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号