I created a website using the new QR Code Generator example. I noticed that the users cannot actually download the generated QR code. Is there a way that I can add a “download” button so the users can download their created QR code?
One way to accomplish this is to upload the SVG that is generated to the Media Manager upon creation of a QR Code. You can then generate a download link based on the fileUrl.
Using the example code you provided, we can add the following functions to qrCode.jsw :
import { mediaManager } from 'wix-media-backend';
export async function getDownloadUrl(fileUrl) {
const getDownloadUrl = await mediaManager.getDownloadUrl(fileUrl);
return getDownloadUrl;
}
export function uploadImage(buffer,fileName) {
//Ensures proper filename is established (no invalid characters)
fileName = fileName.replace(/[/\\?%*:|."<>]/g, '-');
return mediaManager.upload(
"/QR",
buffer,
`${fileName}`,
{
"mediaOptions": {
"mimeType": "image/svg+xml",
"mediaType": "vector"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": true
}
}
);
}
uploadImage() will be used to upload the SVG that is generated via the ‘qrcode’ NPM Package. It returns a fileUrl once the SVG is successfully uploaded.
getDownloadUrl() will then be used to generate a link that will download the SVG to the users local computer.
Next we’ll add a download button to the Home Page:
We will then modify the Page Code (Home) to look like the following snippet:
import { generateQRcode,uploadImage,getDownloadUrl } from 'backend/qrCode.jsw';
let fileUrl,downloadUrl;
$w.onReady(() => {
$w('#generateButton').onClick(async () => {
const qrValue = $w('#qrInput').value;
const qr = await generateQRcode(qrValue);
//Upload QR to Media Manager
const upload = await uploadImage(qr,qrValue);
fileUrl = upload.fileUrl;
//Show QR Code on Client Side
$w('#qrVector').src = qr;
//Adds Download Link to Button
downloadUrl = await getDownloadUrl(fileUrl);
$w('#downloadQR').link = downloadUrl;
});
});
If you need the SVG to be converted to another image file format, or if you would like to download the SVG fully client side, it may be possible via additional NPM Packages and/or a Custom Element.