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:
|
<<: Tutorial on setting up scheduled tasks to backup the Oracle database under Linux
>>: Detailed explanation of MySQL custom functions and stored procedures
Table of contents Purpose Experimental environmen...
Preface In the previous article Detailed Explanat...
Operation effect html <head> <meta chars...
This article shares the specific code of JavaScri...
Preface One of the functions of an interceptor is...
To debug js code, you need to write debugger in t...
In the actual projects I participated in, I found...
It is very simple to build a go environment under...
Table of contents Multiple conditional statements...
Table of contents 1. Instructions for use 2. Prep...
As a Vue user, it's time to expand React. Fro...
According to null-values, the value of null in My...
BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...
1. Why do we need to divide tables and partitions...
1 Pull the image from hup docker pull nginx 2 Cre...