node js upload image and upload to ftp: works for one but not two images
var ftpClient = new ftp();
var form = new formidable.IncomingForm();
form.parse(request, function(err, fields, files) {
if (files.shoplogo.name) {
var dimensions = sizeOf(files.shoplogo.path);
console.log(dimensions.width + " x " + dimensions.height);
if (dimensions.width > 255 || dimensions.height > 255 || dimensions.width < 145 || dimensions.height < 145) {
response.send('<script>alert("Das Bild darf nicht größer 250x250 oder kleiner als 150x150 sein."); history.back(); </script>');
return;
}
if (files.shoplogo.size > 200000) {
response.send('<script>alert("Das Bild darf nicht größer als 150 kb sein."); history.back(); </script>');
return;
} else {
var oldpath = files.shoplogo.path;
ftpClient.on('ready', function() {
ftpClient.put(oldpath, '/web/shopContent/' + 'logo_' + fields.shopid + ".jpg", function(err, list) {
if (err) throw err;
ftpClient.end();
});
});
}
}
if (files.shopcover.name) {
var dimensions = sizeOf(files.shopcover.path);
console.log(dimensions.width + " x " + dimensions.height);
if (dimensions.width > 505 || dimensions.height > 505 || dimensions.width < 245 || dimensions.height < 245) {
response.send('<script>alert("Das Bild darf nicht größer 500x500 oder kleiner als 250x250 sein."); history.back(); </script>');
return;
}
if (files.shopcover.size > 200000) {
response.send('<script>alert("Das Bild darf nicht größer als 150 kb sein."); history.back(); </script>');
return;
} else {
var oldpath = files.shopcover.path;
ftpClient.on('ready', function() {
ftpClient.put(oldpath, '/web/shopContent/' + 'cover_' + fields.shopid + ".jpg", function(err, list) {
if (err) throw err;
ftpClient.end();
});
});
}
}
ftpClient.connect({
'host': 'host',
'user': 'user',
'password': 'pw'
});
});
Working from scratch I managed to code a function that allows me to upload images to my server (from client) and then to upload it to FTP server. If the user selects one image a time everything works fine but uploading both images breaks.
I think the problem is how I handle the connection. How can I tweak the code?
from Recent Questions - Stack Overflow https://ift.tt/3ilgdPc
https://ift.tt/eA8V8J
Comments
Post a Comment