JavaScript 瀹炵幇鏂囦欢涓嬭浇骞堕噸鍛藉悕
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 瀹炵幇鏂囦欢涓嬭浇骞堕噸鍛藉悕相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/type' title='type'>type
size set save onload nav href rev stat绗竴绉嶆槸html瀹樼綉涓殑鏂规硶
<a href="/images/liang.jpg" download="鏂囦欢鍚嶇О">
HTML5 涓?a
鏍囩鎻愪緵浜嗕竴涓?filename
灞炴€э紝鍙互涓嬭浇鎴愭寚瀹氱殑download
灞炴€у悕绉?br />杩欑鍚屾簮璁块棶鏄病鏈夐棶棰樼殑锛屼絾涓€璺ㄥ煙灏变笉琛屼簡锛岃瘯浜嗗叾瀹冩柟娉曪紝涓嶆槸鎶ヨ法鍩熼敊璇紝灏辨槸鍦ㄥ綋鍓嶉〉闈㈡墦寮€鏂囦欢锛屼綋楠岀浉褰撲笉濂姐€?br />
绗簩绉嶆柟妗堟瘮杈冮€氱敤/** * 鑾峰彇 blob * url 鐩爣鏂囦欢鍦板潃 */ function getBlob(url) { return new Promise(resolve => { const xhr = new XMLHttpRequest(); xhr.open(鈥楪ET鈥?span class="token punctuation">, url, true); xhr.responseType = 鈥榖lob鈥?span class="token punctuation">; xhr.onload = () => { if (xhr.status === 200) { resolve(xhr.response); } }; xhr.send(); }); } /** * 淇濆瓨 blob * filename 鎯宠淇濆瓨鐨勬枃浠跺悕绉? */ function saveAs(blob, filename) { if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, filename); } else { const link = document.createElement(鈥榓鈥?span class="token punctuation">); const body = document.querySelector(鈥榖ody鈥?span class="token punctuation">); 銆€銆€銆€ link.href = window.URL.createObjectURL(blob); link.download = filename; // fix Firefox link.style.display = 鈥榥one鈥?span class="token punctuation">; body.appendChild(link); link.click(); body.removeChild(link); window.URL.revokeObjectURL(link.href); } } /** * 涓嬭浇 * @param {String} url 鐩爣鏂囦欢鍦板潃 * @param {String} filename 鎯宠淇濆瓨鐨勬枃浠跺悕绉? */ function download(url, filename) { getBlob(url).then(blob => { saveAs(blob, filename); }); }
以上是关于JavaScript 瀹炵幇鏂囦欢涓嬭浇骞堕噸鍛藉悕的主要内容,如果未能解决你的问题,请参考以下文章
涓€涓猘jax瀹炵幇琛ㄥ崟涓婁紶鏂囦欢鐨勭鍣?formdata
Spring Cloud Alibaba锛堜簩锛?閰嶇疆涓績澶氶」鐩€佸閰嶇疆鏂囦欢銆佸垎鐩綍瀹炵幇