這篇文章主要介紹Vue如何實現文件上傳和下載功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1、a標簽download屬性
在H5中,為a標簽新增了一個download屬性,來直接文件的下載,文件名就是download屬性文件名。
download屬性暫時只支持Google Chrome 和 Mozilla Firefox,其他瀏覽器均不支持該屬性;
download是H5新增的屬性,H5以前沒有該屬性;
2、URL.createObjectURL
URL.createObjectURL()方法會根據傳入的參數創建一個指向該參數對象的URL,這個URL的生命僅存在于它被創建的這個文檔里,新的對象URL指向執行的File對象或者是Blob對象。
File對象,就是一個文件,比如我用input type="file"標簽來上傳文件,那么里面的每個文件都是一個File對象。
Blob對象,就是二進制數據,比如通過new Blob()創建的對象就是Blob對象,又比如在XMLHttpRequest里,如果指定responseType為blob,那么得到的返回值也是一個blob對象。
let URL = window.URL || window.webkitURL; let downloadUrl = URL.createObjectURL(blob || file);
3、URL.revokeObjectURL
URL.revokeObjectURL()方法會釋放一個通過URL.createObjectURL()創建的對象URL,如果不再需要這個對象,就要釋放它,被釋放掉以后,這個對象URL就不再指向指定的文件了。
downloadUrl && URL.revokeObjectURL(downloadUrl);
4、Vue.js上傳和下載文件
<template> <div class="btn-box"> <h4>文件上傳:</h4> <input class="file-input" type="file" @change="getFile($event)" /> <el-button type="primary" @click="upload">上傳文件(POST)</el-button> <h4>文件下載:</h4> <el-button type="primary" @click="downloadLink">下載帶鏈接文件(window.open)</el-button> <el-button type="primary" @click="downloadBlobByGet">二進制流下載(GET)</el-button> <el-button type="primary" @click="downloadBlobByPost">二進制流下載(POST)</el-button> </div> </template> <script> import axios from "axios" export default { name: "attendPoint", data() { return {, file: null, fileName: "test.xlsx" } }, methods: { // 選取文件 getFile(event) { this.file = event.target.files[0]; }, // 上傳文件(POST) upload() { let url = "http://localhost:3000/upload/test"; let formData = new FormData(); formData.append("name", "zhangsan"); formData.append("age", "18"); formData.append("file", this.file); let config = { headers: { "Content-Type": "multipart/form-data" } } axios.post(url, formData, config).then((res) => { this.fileName = res.data.downloadUrl; this.$message.success("上傳成功!"); }).catch(() => { this.$message.error("請先上傳文件!"); }) }, // 下載帶鏈接文件(window.open) downloadLink() { if (this.fileName) { window.open("http://localhost:3000/download/test?fileName=" + this.fileName); } }, // 二進制流下載(GET) async downloadBlobByGet() { let urlGet = "http://localhost:3000/download/test?fileName=" + this.fileName; let fileData = await axios.get(urlGet, { responseType: "blob" }); let URL = window.URL || window.webkitURL; let downloadUrl = URL.createObjectURL(fileData.data); let a = document.createElement("a"); a.href = downloadUrl; a.download = this.fileName;//下載后文件名 a.click(); a = null; downloadUrl && URL.revokeObjectURL(downloadUrl); }, // 二進制流下載(POST) async downloadBlobByPost() { let urlPost = "http://localhost:3000/download/post/test"; let fileData = await axios({ method: "post", url: urlPost, // 請求地址 data: { fileName: this.fileName }, // 參數 responseType: "blob" // 表明返回服務器返回的數據類型 }) let URL = window.URL || window.webkitURL; let downloadUrl = URL.createObjectURL(fileData.data); let a = document.createElement("a"); a.download = this.fileName; a.href = downloadUrl; a.click(); a = null; downloadUrl && URL.revokeObjectURL(downloadUrl); }, }, } </script> <style scoped> .btn-box { padding: 20px; } .el-button, input { max-width: fit-content; display: block; margin: 20px; } </style>
以上是“Vue如何實現文件上傳和下載功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。