Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:50:02+00:00 2026-06-12T12:50:02+00:00

I am trying to convert an HTML5 canvas to an image. This is what

  • 0

I am trying to convert an HTML5 canvas to an image. This is what I got so far:

var tmp_canvas = document.getElementById('canvas');
var dataURL = tmp_canvas.toDataURL("image/png");
$('#thumbnail_list').append($('<img/>', { src : dataURL }).addClass('image'));

but the problem is that I get this code:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAYAAADtt+XCAAAgAElEQVR4nNS6Z1xVaZbvv/c+CVOZc6mYEMlJMZRizgljGRARs6AgOSMGQATBSM5ZyTkoOQkSzJWrp3t6etLt6Z7pmf/c++L7f3EOiBZW2dM9dz73xfdzztl7n3Oe/Txrrd9a69mCTC4gkwvIZAKSTECUBARRQBA+jii+46f.......class="image">

I want a normal image path that the user can download!

Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:50:03+00:00Added an answer on June 12, 2026 at 12:50 pm

    Info: IE10+ doesn’t support below method at all. Other people already did the work and implemented cross browser solutions.
    This is one of them.

    First, add the generated data URL to the href attribute of an <a> tag.
    However on some browsers, this alone will not trigger a download. Instead it will open the linked image in a new page.

    Download dialog for a base64 image:

    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...." class="image" />
    

    Based on above example, convert the MIME type of the data URL to this:

    <a href="data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUg....">Download</a>
    

    By telling the browser that the data are application/octet-stream, it will ask you to save it on your hard-disk.


    Specifying a filename:

    As Adi said in the comments below, there is no standard way to define a filename using this method. But, there are two approaches which might work in some browsers.

    A) The download attribute introduced by Google Crome

    <a download="image.png" href="...">
    

    B) Defining HTTP headers within the data URL
    headers=Content-Disposition: attachment; filename=image.png

    <a href="data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=image.png;base64,iVBORw0KGgoAAAA">
    

    This worked at least in some older versions of Opera.
    Here is some discussion about this.

    Looking into the Bug/Feature-Tracking systems of the major browsers shows that defining a filename is quite a big wish of the community. Maybe we will see a cross-browser compatible solution in near future! 😉


    Save RAM and CPU ressources:

    If you don’t want to bloat the RAM of your visitor’s browser, you can also generate the data URL dynamically:

    <a id="dl" download="Canvas.png">Download Canvas</a>
    
    function dlCanvas() {
        var dt = canvas.toDataURL('image/png');
        this.href = dt;
    };
    dl.addEventListener('click', dlCanvas, false);
    

    This way, your canvas may still be shown as an image file by your browser.
    If you want to increase the probability to open a download dialog, you should extend the function above, so that it does the replacement as shown above:

    function dlCanvas() {
        var dt = canvas.toDataURL('image/png');
        this.href = dt.replace(/^data:image\/[^;]/, 'data:application/octet-stream');
    };
    dl.addEventListener('click', dlCanvas, false);
    

    Last, add the HTTP header to make extra sure that most browsers offer a valid filename to you! 😉


    FULL EXAMPLE:

    var canvas = document.getElementById("cnv");
    var ctx = canvas.getContext("2d");
    
    /* FILL CANVAS WITH IMAGE DATA */
    function r(ctx, x, y, w, h, c) {
      ctx.beginPath();
      ctx.rect(x, y, w, h);
      ctx.strokeStyle = c;
      ctx.stroke();
    }
    r(ctx, 0, 0, 32, 32, "black");
    r(ctx, 4, 4, 16, 16, "red");
    r(ctx, 8, 8, 16, 16, "green");
    r(ctx, 12, 12, 16, 16, "blue");
    
    /* REGISTER DOWNLOAD HANDLER */
    /* Only convert the canvas to Data URL when the user clicks. 
       This saves RAM and CPU ressources in case this feature is not required. */
    function dlCanvas() {
      var dt = canvas.toDataURL('image/png');
      /* Change MIME type to trick the browser to downlaod the file instead of displaying it */
      dt = dt.replace(/^data:image\/[^;]*/, 'data:application/octet-stream');
    
      /* In addition to <a>'s "download" attribute, you can define HTTP-style headers */
      dt = dt.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=Canvas.png');
    
      this.href = dt;
    };
    document.getElementById("dl").addEventListener('click', dlCanvas, false);
    <canvas id="cnv" width="32" height="32"></canvas>
    <a id="dl" download="Canvas.png" href="#">Download Canvas</a>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert a HTML string into a .PNG file. Thanks to
I'm trying to convert from HTML to Latex, and want to change this: <a
I'm trying to convert programatically PDF to HTML. So far I've been using pdftohtml
I have been trying to convert sdot like this html_entity_decode(&sdot;); Any tips?
I am trying to play with google's swiffy to convert as2 content into html5.
I've been trying to re-implement an HTML5 image uploader like the one on the
I am trying to convert this HTML table: Code: <table id=students border=1> <thead> <tr>
I have this psd and trying to convert it to html css. But only
I am new to HTML5 <canvas> and was trying to make something, actually draw
I am trying to convert HTML into a PDF document in Django and haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.