1

I am working on a QR Code project and some image is generated via Canvas. So they are not actually images. And users can't download/save it by hitting a "save as" context menu item.

How do I let the users able to download it?

2
  • What have you tried so far? How are you generating the image on the canvas? Commented Jan 8, 2014 at 4:20
  • window.open(canvas.toDataURL()) Commented Jan 8, 2014 at 5:14

3 Answers 3

1

Assuming your "Save as" button look something like this (uninitialized, see below):

<a href="#" id="saveas" download="">Save as</a>

Then when you have created the QR code on canvas do the following to initialize it:

/// QR created here..

var link = document.getElementById('saveas');

link.download = 'filename.png';  /// set a filename or a default
link.href = canvas.toDataURL();  /// create a data-uri as link

The key here is the new download attribute for the anchor tag. This will pop up a save as requester instead of navigating to the image.

You can also chose to hide the link and show it when it's initialized. You get the idea..

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, canvas.toDataURL() is a native supported method?
@AwQiruiGuo yes, it's native and part of the canvas standard.
0
var image = new Image();
image.src = canvas.toDataURL("image/png");
document.body.appendChild(image); // or whereever you want.

edit: onload event handler is only required for server-loaded images.

Comments

0

// create save tag then

var data = canvas.toDataURL(); // default is png add param image/jpg if you want to document.getElementById('save').href = data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.