I'm able to download the image using http and and I am able to resize it using imagemagic, but I can't figure out how to upload it to Parse. Parse only lets me upload: an array of byte value numbers OR base64 string. What I'm confused on is how I can convert the stdout to base64 string so that I can upload it to parse. I tried using fs, but to no avail. I just keep getting errors when it tries to read the file and convert it. What is wrong with my code?
var _ = require('underscore');
var url = require("url");
var srcUrl = 'https://i.ytimg.com/vi/JxwwGtquGqw/maxresdefault.jpg';
var http = srcUrl.charAt(4) == 's' ? require("https") : require("http");
var im = require('imagemagick');
var Image = require("parse-image");
var image_card_2x = [540, 350];
var thumb = '';
var fs = require('fs');
var request = http.get(url.parse(srcUrl), function(response) {
console.log("got data");
var data = '';
response.setEncoding('binary');
response.on('data', function(chunk) {
data += chunk;
});
console.log("data is " + data);
response.on('end', function () {
// var image = new Image();
// image.setData(response.buffer);
// image.scale({
// width: image_card_2x,
// height: Math.floor((image.height() * arrayElement[0]) / image.width())
// });
// image.setFormat("JPEG");
// console.log("about to make image");
im.resize({
srcData: data,
width: 404,
height: 269,
format: 'jpg'
}, function(err, buffer) {
if (err) throw err;
var bitmap = fs.readFileSync(file);
var base64 = new Buffer(bitmap).toString('base64');
var file = new Parse.File("maa.jpg", {
base64: base64
});
console.log("about to save");
var VideoLinks = Parse.Object.extend("VideoLinks");
var videoLink = new VideoLinks();
videoLink.set("image", file);
return videoLink.save();
console.log("saved");
//fs.writeFileSync('kittens-resized.jpg', stdout, 'binary');
console.log("worked!");
});
})
});
Related
i have code to make pdf and succeeded in downloading and opening it, but i want to send pdf to my server on node js, and i have made app.post on server but i can't make pdf become base64 and save it on server
in frontend
<script type="text/javascript">
function genPDF() {
html2canvas(document.getElementById('testDiv')).then(function (canvas) {
var img = canvas.toDataURL('image/png');
var doc = new jsPDF('landscape');
doc.addImage(img, 'png', 10, 10);
var temp = doc.save('test.pdf');
var post = new XMLHttpRequest();
post.open("POST", "/receive");
post.send(temp);
}
</script>
Download PDF
in server
app.post('/receive', function (request, respond) {
var body = '';
var filePath = './static' + '/document/Document.pdf';
//
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var data = body.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
fs.writeFile(filePath, buf, function (err) {
if (err) throw err
respond.end();
});
});
});
how to send var temp = doc.save('test.pdf'); server and generate pdf to base64?
Use the below code this will help you.
IN FE
<script type = "text/javascript">
function genPDF() {
html2canvas(document.getElementById('testDiv')).then(function (canvas) {
var img = canvas.toDataURL('image/png');
var doc = new jsPDF('landscape');
doc.addImage(img, 'png', 10, 10);
var temp = doc.save('test.pdf');
var data = new FormData();
data.append("pdf_file", temp);
var post = new XMLHttpRequest();
post.open("POST", "/receive");
post.send(data);
}
</script>
<a href = "javascript:genPDF()" > Download PDF </a>
IN BE
const fs = require('fs');
const multipartMiddleware = require('connect-multiparty')();
const express = require('express');
const app = express();
const port = 8000;
const filePath = './static' + '/document/Document.pdf';
app.post('/', multipartMiddleware, (request, response) => {
fs.readFile(request.files.pdf_file.path, (err, data) => {
fs.writeFile(filePath, data, function (err) {
if (err) throw err;
response.send('Done')
});
})
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
I have tried decode image file in node js with body parser in and uploaded to server url. but I didn't get success in uplaod and parser image with base64 image.
if (req.method === 'POST') {
//base64_decode(req.body.profileImg, 'copy.jpeg');
//console.log(req.body.profileImg);
var NewImageName = Math.random().toString(36).substring(7);
var imageBuffer = decodeBase64Image(req.body.profileImg);
fs.writeFile('../assets/images/seller/'+NewImageName+'.png', imageBuffer.data, function(err) {
});
You can try this code for decoding the base64 image.
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
}
app.post('/UploadImages', function (req,res){
if (req.method === 'POST') {
var NewImageName = Math.random().toString(36).substring(7);
var imageBuffer = decodeBase64Image(req.body.profileImg);
fs.writeFile('../assets/images/seller/'+NewImageName+'.png', imageBuffer.data, function(err) {
});
res.json(200, {profileImgName: NewImageName });
}
});
Just get your base64 encoded string in you nodeJs function and you can send it to the function decodeBase64Image i have created above which will decode the Image and then you can upload it.
Thanks
I am sending base64data of canvas to node.js script. I need the base64data to be stored as an image to the s3bucket. Is there any way to achieve it?
Store your Data URI in a variable.
Create function which decodes your data URI(64 bit encoded string) to string(Here I have created dataURItoBlob() function) and after decoding return the string.
Pass that string to in body of S3 upload function.
var myDataUri = "data:image/jpg;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYW..."
var myFile=dataURItoBlob(myDataUri);
function dataURItoBlob(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {
type: 'image/jpg'
});
}
if (myFile)) {
results.innerHTML = '';
var params = {
Key: fileName+'.jpg',
ContentType: 'image/jpg',
Body: myFile
};
bucket.upload(params, function(err, data) {
results.innerHTML = err ? 'ERROR!' : 'UPLOADED.: ' + file;
});
} else {
results.innerHTML = 'Nothing to upload.';
}
you can send base64 data with AWS putObject method as follows
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./s3_config.json');
var s3Bucket = new AWS.S3( { params: {Bucket: 'myBucket'} } );
var imageBase64Data='Your base64 code '
s3Bucket.putObject(imageBase64Data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
});
I am new to Node.js world, kind of stuck in situation.
below code is for reference:
var http = require('http');
var step = require('step');
var request = require('request');
exports.readimage2 = function(req, res){
//res.send(200,'OK');
//var image_url = 'http://www.letsgodigital.org/images/artikelen/39/k20d-image.jpg'; //--- 10mb
//var image_url = 'http://upload.wikimedia.org/wikipedia/commons/2/2d/Snake_River_(5mb).jpg';
//var image_url = 'http://www.sandia.gov/images2005/f4_image1.jpg'; //--- 2mb
var image_url = 'http://www.fas.org/nuke/guide/pakistan/pakistan.gif'; // --- some KB
http.get(image_url,
function(responseData) {
var data = new Buffer(parseInt(responseData.headers['content-length'],10));
var pos = 0;
responseData.on('data', function(chunk) {
chunk.copy(data, pos);
pos += chunk.length;
});
responseData.on('end', function () {
res.send(200, data);
});
});
};
Above code fails working for large files if i use it with step module.
Anyone suggest how to do it properly with step.
Here how i did it using step..... although the request module did same for image buffer download thanks to a post on stackoverflow just need to set encoding to null in request to work for buffer response.
var canvas = new Canvas(3000, 3000),
ctx = canvas.getContext('2d'),
Image = Canvas.Image;
var image_url = "http://www.a2hosting.com/images/uploads/landing_images/node.js-hosting.png";
//var image_url = 'http://upload.wikimedia.org/wikipedia/commons/1/16/AsterNovi-belgii-flower-1mb.jpg';
step(
function() {
request.get({
url: image_url,
encoding: null
}, this);
},
function(err, response, body) {
var img = new Image;
img.src = body;
ctx.drawImage(img, 0, 0, img.width, img.height);
//res.send(200, data);
res.send(200, '<img src="' + canvas.toDataURL() + '" />');
}
);
Below is the code working for simple http module of node.
var http = require('http');
var step = require('step');
var request = require('request');
exports.imagedownload = function(req, res){
step(
function(){
console.log('*********** image download start ***********');
fndownload(this);
},
function(err, result){
if(err) {
}
console.log('*********** image download end ***********');
res.send(200, result);
}
);
};
function fndownload(callback) {
var image_url = 'http://upload.wikimedia.org/wikipedia/commons/2/2d/Snake_River_(5mb).jpg'; // --- some KB
http.get(image_url,
function(responseData) {
var data = new Buffer(parseInt(responseData.headers['content-length'],10));
var pos = 0;
responseData.on('data', function(chunk) {
chunk.copy(data, pos);
pos += chunk.length;
});
responseData.on('end', function () {
//res.send(200, data);
callback(null, data);
});
});
};
I'm trying to read the contents of a bunch of javascript files on a server, and then concatenate them into a new local file. The files have to be concatenated in a specific order (specified in an array). Here's what I have so far:
var http = require('http');
var fs = require('fs');
var commonWebFiles = getCommonWebDependenciesInOrder();
var fileContents = [];
var path = '/folder/';
fs.mkdir("target");
for(var i = 0, l = commonWebFiles.length; i < l; ++i){
getFileContents(path, commonWebFiles[i]);
}
function getCommonWebDependenciesInOrder(){
//Hit manager to get an correctly ordered array of common web dependencies
//Stub
return [
'file1.js',
'file2.js',
'file3.js'
];
};
function getFileContents(path, filename){
var contents = "";
var writeStream = fs.createWriteStream("target/" + filename, {'flags': 'a'});
var options = {
host: 'ahost.net',
port: 80,
path: path + filename
};
var req = http.get(options, function(res) {
res.on('data', function(chunk) {
contents += chunk;
});
res.on('end', function() {
writeStream.write(contents, encoding='binary');
writeStream.end();
fileContents[filename] = contents;
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};
This downloads the files and recreates them locally, but it seems a little clunky. When I tried to just write a single file directly from a looped set of requests, I got the chunks out of order....I feel like there must be an easier way....
Thanks in advance.
Use async and request:
var fs = require('fs'),
async = require('async'),
request = require('request');
// Utility function to overcome request's callback (err, response, body) where we're only interested in err and body
function simpleRequest(url, callback) {
request(url, function(err, response, body) {
callback(err, body);
});
}
async.map(urls, simpleRequest, function(err, results) {
if(err)
return console.error(err);
fs.writeFile(outfile, results.join(''));
});