Answer the question
In order to leave comments, you need to log in
NodeJS+ImageMagick+Multipart form data, what could be causing the problem?
What is the point: I am writing a method for loading several images, renaming them along the way, taking the hash received from the image as a new name. But since I have little experience with the file system from NodeJS, I ran into difficulties... Here is my code:
app.post('/upload', function(req, res){
var photos = [];
for (var l = req.files.files.length; l--; ) {
(function(l){
im.resize({
srcData: fs.readFileSync(req.files.files[l].path, 'binary'),
width: 300,
height: 400,
format: 'jpg',
}, function(err, stdout, stderr){
if (err) {
return console.error(err);
} else {
fs.writeFileSync(req.files.files[l].path, stdout, 'binary');
var hash = crypto.createHash('md5'),
rdd = fs.createReadStream(req.files.files[l].path);
rdd.on('end', function () {
var newfilename = '';
hash.end();
newfilename = hash.read() + '.jpg';
console.log(newfilename);
photos.push(newfilename);
fs.rename(req.files.files[l].path,
__dirname + '/pub/img/' + newfilename);
rdd.close();
fs.unlinkSync(req.files.files[l].path);
!l && res.redirect('/#!/item/'+req.body.id);
});
rdd.pipe(hash);
}
});
})(l);
}
});
Error: ENOENT, open '/home/m0sk1t/projects/sorm/tmp/pHNhXY2MQ-Ib_WeWp7A06A0V.jpg'
at Error (native)
app.post('/upload', function(req, res){
var photos = [];
console.log(req.body.id);
for (var l = req.files.files.length; l--; ) {
(function(l){
im.resize({
srcData: fs.readFileSync(req.files.files[l].path, 'binary'),
width: 300,
height: 400,
format: 'jpg',
}, function(err, stdout, stderr){
fs.writeFile(req.files.files[l].path, stdout, 'binary', function(err){
if (err) return console.error(err);
var hash = crypto.createHash('sha1'),
rdd = fs.createReadStream(req.files.files[l].path);
rdd.on('end', function () {
var newfilename = '';
rdd.close();
newfilename = hash.digest('hex') + '.jpg';
console.log(newfilename);
photos.push(newfilename);
fs.rename(req.files.files[l].path,
__dirname + '/pub/img/' + newfilename, function(){
!l && res.redirect('/#!/item/'+req.body.id);
});
});
rdd.on('data', function(d) {
hash.update(d);
});
});
});
})(l);
}
});
Answer the question
In order to leave comments, you need to log in
At least your fs.rename is asynchronous and you immediately delete the file synchronously, it is logical that rename will have nothing to move.
PS to use synchronous functions - moveton (plus falling of productivity). Better rewrite to asynchronous with execution control.
And the code is shit...read the clean code and rewrite it normally
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question