vue.js downloads pictures according to picture url

vue.js downloads pictures according to picture url

Recently, when I was working on a front-end vue.js docking function module, I needed to implement a function to download pictures. The backend returned a string of picture URLs. I tried many methods and found that the effect when clicking download was to jump to a new picture web page. After some thinking, I finally solved this problem:

This is the json data returned by the backend (IP address is coded to prevent leakage of important information):

My reference in html is like this:

<a @click="downCom" >
                Download License<i class="icon-down"></i>
              </a>

Download image method in vue.js method:

downCom() {
      let that = this;
      this.$http.files().then(res => {
        let hreLocal="";
        hreLocal = res.data.data.url;
        this.downloadByBlob(hreLocal,"pic")

      });
    },

The following method can be used directly. Just pass your image URL to this method, and you can implement vue.js to download the image.

downloadByBlob(url,name) {
    let image = new Image()
    image.setAttribute('crossOrigin', 'anonymous')
    image.src = url
    image.onload = () => {
      let canvas = document.createElement('canvas')
      canvas.width = image.width
      canvas.height = image.height
      let ctx = canvas.getContext('2d')
      ctx.drawImage(image, 0, 0, image.width, image.height)
      canvas.toBlob((blob) => {
        let url = URL.createObjectURL(blob)
        download(url,name)
        // Release the URL object after use URL.revokeObjectURL(url)
      })
    }
  },

The download(url,name) method called:

function download(href, name) {
  let eleLink = document.createElement('a')
  eleLink.download = name
  eleLink.href = href
  eleLink.click()
  eleLink.remove()
}

After completing the above code, you can download pictures instead of browsing them.

Finally, you can successfully click to download the picture, the effect is as follows:

This is the end of this article about vue.js downloading pictures according to picture url. For more relevant vue.js picture url downloading content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript data transmission between different pages (URL parameter acquisition)
  • How to remove jsessionid after URL in Springboot
  • Some wonderful uses of URL objects in JavaScript
  • JavaScript library urlcat URL builder library

<<:  Tutorial on setting up scheduled tasks to backup the Oracle database under Linux

>>:  Detailed explanation of MySQL custom functions and stored procedures

Recommend

Detailed example of using case statement in MySQL stored procedure

This article uses an example to illustrate the us...

Linux type version memory disk query command introduction

1. First, let’s have a general introduction to th...

Web Design: Script Materials Reconstruct User Experience

<br />Original text: http://blog.rexsong.com...

Html makes a simple and beautiful login page

Let’s take a look first. HTML source code: XML/HT...

How to choose the right MySQL datetime type to store your time

When building a database and writing a program, i...

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

Mysql index types and basic usage examples

Table of contents index - General index - Unique ...

The meaning and calculation method of QPS and TPS of MySQL database

When doing DB benchmark testing, qps and tps are ...

MySQL download and installation details graphic tutorial

1. To download the MySQL database, visit the offi...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...