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

  • Home
  • SEARCH
  • 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 9248261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:54:23+00:00 2026-06-18T09:54:23+00:00

I am passing base64 encoded data from a canvas element to an express handler

  • 0

I am passing base64 encoded data from a canvas element to an express handler that saves the data into a png file

What should I set contentType to in my ajax call below? (Using the default i.e not x-www-form-urlencoded gives me a png file that doesn’t open)

$("#save").click(function(){
         var canv = document.getElementById('imageView');
         var canvData = canv.toDataURL('image/png');
       console.log("the type is " + typeof canvData);

          $.ajax({

          type: "POST",
          url: '/upload',
          data: canvData,
          contentType: '???', 
          success: function(data){

            console.log(data);

          }         
         });



      });

For completeness my express handler is here:

// insert an image
function objToString (obj) {
    var str = '';
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            str += p + '::' + obj[p] + '\n';
        }
    }
    return str;
}
var fs = require('fs');

app.post('/upload', function(req, res){



    var image = req.body;
    image = objToString(image);
    var noHeader = image.substring(image.indexOf(',') + 1);
    var decoded = new Buffer(noHeader, 'base64');

    fs.writeFile('testfile.png', decoded, function(err){

        res.send("without header " + noHeader + "decoded " + decoded);

    });

});
  • 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-18T09:54:23+00:00Added an answer on June 18, 2026 at 9:54 am

    The data URL you’ll get is a base64 string like:

     data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY....
    

    The specs mention the toBlob method in the canvas API to directly get the data as raw PNG. However, it’s not implemented yet in the latest version of Chrome, so I wouldn’t use it as-is.

    If you want to get the raw PNG data (and thus use the image/png Content-Type), you can use this toBlob polyfill.

    EDIT: After double-checking my code, I send the Data URL as JSON.

    In the client:

    $.ajax({
        url: '/api/1/activities/' + self.model.id + '/attachments/' + fileName,
        type: 'PUT',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify({data: canv.toDataURL('image/png'}),
        success: function () { ... }
    });
    

    In the server:

    function parseDataURL(body) {
      var match = /data:([^;]+);base64,(.*)/.exec(body);
      if(!match)
        return null;
    
      return {
        contentType: match[1],
        data: new Buffer(match[2], 'base64')
      };
    }
    
    app.post('/upload', function (req, res) {
      var upload = parseDataURL(req.body.data);
      ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to play video by somehow passing data from a File object
I'm working on an app that needs to write a .png image file from
passing post data using cURL requires that the name of the input. However, I
Possible Duplicate: Passing base64 encoded strings in URL I am working on creating a
I was parsing an XML that has an image that is Base64 encoded. I
I'm building an application that allows the user to POST HTML5 canvas data that
I am passing base64 encoded image string to the server side with the HTTP
Is it possible to use a RegEx to validate, or sanitize Base64 data? That's
Whether passing a base64 encoded username and passwords as variables through a secured SSL
I have an image on a web page constructed like so: <img src=data:image/png;base64;.... />

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.