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

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

CSS3 to achieve floating cloud animation

Operation effect html <head> <meta chars...

Realizing provincial and municipal linkage effects based on JavaScript

This article shares the specific code of JavaScri...

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

Detailed steps for debugging VUE projects in IDEA

To debug js code, you need to write debugger in t...

Some methods to optimize query speed when MySQL processes massive data

In the actual projects I participated in, I found...

A brief discussion on how to write beautiful conditional expressions in JS

Table of contents Multiple conditional statements...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

React sample code to implement login form

As a Vue user, it's time to expand React. Fro...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Mysql database scheduled backup script sharing

BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...

How to run nginx in Docker and mount the local directory into the image

1 Pull the image from hup docker pull nginx 2 Cre...