企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
正常可以用a链接+download属性(只支持同源文件,非同源会直接打开不会下载) 但是。图片类的会直接打开这张图片,不是下载图片。 解决方法:canvas转base64后下载 ``` <a class="download" v-for="(x,index) in content.content_info.download" :key="index" :href="gethref(x.file_url)" :download="x.file_url" @click="downloadImg(x)"> </a> downloadImg(data){//下载图片 var reg = new RegExp(/(.png|.jpg|.jpeg|.gif)/g, 'i'); if(data && data.file_name && data.file_name.match(reg) && data.file_name.match(reg).length>0){ // 图片转base64地址 var fixtype = function () { var r = data.file_name.match(reg); return 'image/' + r; } var images = new Image(); var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); images.crossOrigin = 'anonymous'; images.src = data.file_url images.onload=function(){ var _w = images.naturalWidth; var _h = images.naturalHeight; canvas.width = _w canvas.height = _h context.drawImage(images,0,0); var a = document.createElement('a'); a.href = canvas.toDataURL(fixtype()); //将画布内的信息导出为png图片数据 a.download = data.file_name; //设定下载名称 a.click(); //点击触发下载 } } }, gethref(url){//根据url判断是否是图片。如果是图片,给a的href设置成“javascript:void(0)” var reg = new RegExp(/(.png|.jpg|.jpeg|.gif)/g, 'i'); if(url && url.match(reg) && url.match(reg).length>0){ return 'javascript:void(0)'; }else{ return url; } } ``` 下载文本 ``` var iframe = null; var iframedoc = null; if (document.all){//IE iframe = document.frames["iframe"]; iframedoc = document.frames["iframe"].document; }else{//Firefox iframe = document.getElementById("iframe"); iframedoc = document.getElementById("iframe").contentDocument; } var url = 'http://pbp38mcp7.bkt.clouddn.com/15441697177232.py'; if (!iframe) return; iframe.url = url; var filename = 'hello.py' var a = document.createElement('a') var blob = new Blob([iframe.contentWindow.document.body.innerHtml]) a.download = filename a.href = URL.createObjectURL(blob) a.click() URL.revokeObjectURL(blob) ```