Express receive Base64 encoded data and file storage __ajax

Source: Internet
Author: User
Tags base64

Turn from: http://marshal.easymorse.com/archives/4935

The picture is manipulated in the browser's JavaScript and can only be Base64 encoded text. For example, storing pictures from HTML5 canvas can only generate Base64 encoded text through the Todataurl () method.
The generated input looks like this:
data:image/png;base64,ivborw0kggoaaaa......ggg==

Among them, the Png;base64, the latter part, is the picture data base64 encoding representation.
We can send the data to the server side via backbone sync, see: Backbone and express integration.
Then, you need to use Express to convert the data to binary data and save it to a file with the following code:

App.post ('/items ', function (req, res) {
Console.log (Req.body.title);
var base64data=req.body.imgdata.replace (/^data:image\/png;base64,/, "");
var binarydata=new Buffer (base64data, ' base64′ '). toString (' binary ');
Require (' FS '). WriteFile (' Out.png ', binarydata, ' binary ', function (err) {
if (err) {
Console.log (ERR);
}
});
Res.send (' saved. ');
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.