scenario

A button

By link (can be custom, can be background upload)

Download one or more files

Download a file

To download a file, click on the A label

  async fileDownload() {
      await fileDownload().then((res) = > {
       // Create a tag
        const link = document.createElement('a')
       // Set the href of the a tag (click address)
        link.href =
          res.data && res.data.data ? res.data.data.vApplicationUrl : ' '
       // Set the attributes of the A tag
        link.setAttribute('download'.'Register application template. XLSX')
       // Click the A TAB

        document.body.appendChild(link)
        link.click()
       // Remove the a tag
        document.body.removeChild(link)
      })
    },
Copy the code

Downloading multiple files

When downloading multiple files, you need to place the links to be downloaded into an array

    downloadFile(url) {
      const iframe = document.createElement('iframe')
      iframe.style.display = 'none' // Prevent impacting the page
      iframe.style.height = 0 // Prevent impacting the page
      iframe.src = url
      document.body.appendChild(iframe) // The iframe must be attached to the DOM tree to send the request
      // Delete the link after 5 minutes.
      setTimeout(() = > {
        iframe.remove()
      }, 5 * 60 * 1000)},async fileDownload() {
      await fileDownload().then((res) = > {
        console.log(res.data.data.vApplicationUrl)
        // Build the linked array
        let link1 =
          res.data && res.data.data ? res.data.data.vApplicationUrl : ' '

        let link2 = res.data && res.data.data ? res.data.data.vRoleUrl : ' '
        let linkArr = []
        linkArr.push(link1, link2)
        // Download the file for each link in the array
        for (let i = 0; i < linkArr.length; i++) {
          // Loop over to call the downloadFile method
          const url = linkArr[i]
          this.downloadFile(url)
        }
      })
    },
Copy the code