Js下載音樂文件


Js下載 音樂文件

    down () {
      // eslint-disable-next-line no-unused-vars
      let downUrl = this.$refs.audio.src // 音樂地址 : 例如: http://m10.music.126.net/20201119111830/51f6bbc51ea067e9d258fa73183b16b1/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/2828582250/29aa/7bcc/f324/6699f160b0c39b010e6e009d271e4948.mp3
      // eslint-disable-next-line no-unused-vars
      let fileName = playerApi.downFileName // 文件名設置: 起風了
      // eslint-disable-next-line no-unused-vars
      axios({
        method: 'get',
        url: downUrl,
        responseType: 'blob',
        headers: {'content-type': 'audio/mpeg'}
        // headers: {'content-length': '4066786', 'content-type': 'audio/mpeg'}
      }).then((res) => {
        // eslint-disable-next-line no-unused-vars
        let blobType = 'application/force-download' // 設置blob請求頭
        // eslint-disable-next-line no-unused-vars
        let blob = new Blob([res.data], {type: res.data.type}) // 創建blob 設置blob文件類型 data 設置為后端返回的文件(例如mp3,jpeg) type:這里設置后端返回的類型 為 mp3
        let downa = document.createElement('a') // 創建A標簽
        // eslint-disable-next-line no-unused-vars
        let href = window.URL.createObjectURL(blob) // 創建下載的鏈接
        downa.href = href // 下載地址
        downa.download = fileName // 下載文件名
        document.body.appendChild(downa)
        downa.click() // 模擬點擊A標簽
        document.body.removeChild(downa) // 下載完成移除元素
        window.URL.revokeObjectURL(href) // 釋放blob對象
      }).catch(function (error) {
        console.log(error)
      })
    },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM