VUE下载文件重命名处理方法


下载远程文件时需要对文件重命名,可以通过如下方法处理,远程链接需要支持跨域。


       /**

        *  处理点击下载按钮调用

        **/

        handleDownloadFlie(flie) {
            this.getBlob(flie.url).then(blob => {
                this.DownloadItem(blob, flie.name);
            });
        },


       /**

        *  通过传入的url拿到blob对象

        **/

       getBlob(url) {
            return new Promise(resolve => {
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.responseType = 'blob';
                xhr.onload = () => {
                    if (xhr.status === 200) {
                        resolve(xhr.response);
                    }
                };
                xhr.send();
            });
        },


       /**

        *  执行文件下载操作

        **/

        DownloadItem(blob, fileName) {
            const link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = fileName;
            link.click();
        }





注:本文转载自网络,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。