问题
I'm really really new to Angular and all. So I've gotten a QR code image from a library (angularx-qrcode) generator.
Here's the code to get the image:
<qrcode [qrdata]="'Your QR code data string'" [size]="256" [level]="'M'"></qrcode>
Now I wanna have a button that allows the user to save the above image locally. How can I achieve this?
Also is the syntax different for different Angular versions (e.g. 2 vs 7)?
Thank you so much!! any help would be appreciated:)
回答1:
Now I wanna have a button that allows the user to save the above image locally. How can I achieve this?
So, you want to download the Qr code image into your local device
CHECK WORKING STACKBLITZ
This is my approach on how I did it!
- First, you need to get the base64 image data from the generated image
- Convert the base 64 encoded image into blob data
- Add a button to download the image
Your component.html
can be something like this:~
<qrcode #parent [qrdata]="qrdata" [size]="256" [level]="'M'"></qrcode>
<br>
<button (click)="saveAsImage(parent)">Download QR Code Image</button>
Your component.ts
can be something like this:~
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
qrdata = 'Initial QR code data string';
saveAsImage(parent) {
// fetches base 64 date from image
const parentElement = parent.el.nativeElement.querySelector("img").src;
// converts base 64 encoded image to blobData
let blobData = this.convertBase64ToBlob(parentElement);
// saves as image
if (window.navigator && window.navigator.msSaveOrOpenBlob) { //IE
window.navigator.msSaveOrOpenBlob(blobData, 'Qrcode');
} else { // chrome
const blob = new Blob([blobData], { type: "image/png" });
const url = window.URL.createObjectURL(blob);
// window.open(url);
const link = document.createElement('a');
link.href = url;
link.download = 'Qrcode';
link.click();
}
}
private convertBase64ToBlob(Base64Image: any) {
// SPLIT INTO TWO PARTS
const parts = Base64Image.split(';base64,');
// HOLD THE CONTENT TYPE
const imageType = parts[0].split(':')[1];
// DECODE BASE64 STRING
const decodedData = window.atob(parts[1]);
// CREATE UNIT8ARRAY OF SIZE SAME AS ROW DATA LENGTH
const uInt8Array = new Uint8Array(decodedData.length);
// INSERT ALL CHARACTER CODE INTO UINT8ARRAY
for (let i = 0; i < decodedData.length; ++i) {
uInt8Array[i] = decodedData.charCodeAt(i);
}
// RETURN BLOB IMAGE AFTER CONVERSION
return new Blob([uInt8Array], { type: imageType });
}
}
Also, if you want to save the text inside the QRCode check this one below:~
CHECK WORKING STACKBLITZ
回答2:
I did not get actually what you want but as i see you want to grab an image from storage and provide it to qrdata
and vice versa
Storage in Angular
You might use localStorag
Create a simple service contains the following code
import { Injectable } from '@angular/core';
@Injectable()
export class MemoryService {
constructor() {}
set(key: string, data: any): void {
try {
localStorage.setItem(key, JSON.stringify(data));
} catch (e) {
console.error(e);
}
}
get(key: string) {
try {
return JSON.parse(localStorage.getItem(key));
} catch (e) {
console.error(e);
return null;
}
}
}
Return back to your qrcode
component
// File: example.ts
export class QRCodeComponent {
public myAngularxQrCode: string = null;
constructor (private memory: MemoryService) {
// assign a value
// this.myAngularxQrCode = 'Your QR code data string';
// here you need to get the value from storage and assign it to your variable
this.myAngularxQrCode = this.memory.get('YOUR OWN KEY');
}
storeQRData() {
this.memory.set('YOUR OWN KEY' , this.myAngularxQrCode);
}
}
// File: example.html
<qrcode [qrdata]="myAngularxQrCode" [size]="256" [level]="'M'"></qrcode>
<button (click)="storeQRData()">Save To Storage</button>
来源:https://stackoverflow.com/questions/55979501/saving-an-image-retrieved-from-external-library-locally-angular