SoFunction
Updated on 2025-04-05

How to implement the image download in vue by clicking to download

The method of clicking to download images in Vue can be implemented using the download attribute of the <a> tag in HTML5 or through JS.

Use the download attribute of the <a> tag to set it directly in the html, for example:

&lt;a href="/" rel="external nofollow"  download&gt;Download the picture&lt;/a&gt;

Through JS implementation, you can use the method of creating URL objects, the code is as follows:

// Download the picture   async downloadImage(e) {
     // Get picture objects and canvas objects     const imgUrl = /
     const response = await fetch(imgUrl)
     const blob = await ()
     // Create a download link     const url = (blob)
     const link = ('a')
      = url
      = ''
     (link)
     ()
     (link)
     // Release the Blob URL     (url)
   }

The above code obtains the image through fetch and converts it into a blob type, then uses the creation of the download link, and uses the click() method of the created <a> tag to achieve the download, and finally removes the newly created <a> tag and the already used URL object.

Summarize

This is the article about clicking to download pictures in vue. This is all about this article. For more related vue clicking to download pictures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!