i have a proyect that im trying to finish i wanna upload images i can upload the images but not in the dir that supose to upload
ok lets code:
app.post('/register',function(req,res)
{
var form = new formidable.IncomingForm();
form.keepExtensions = true;
form.uploadDir ='./uploaded/';
form
.on('error', function(err) {
throw err;
})
.on('field', function(field, value) {
//receive form fields here
})
/* this is where the renaming happens */
.on ('fileBegin', function(name, file){
//rename the incoming file to the file's name
file.path = form.uploadDir + "/" + file.name;
})
.on('file', function(field, file) {
//On file received
})
.on('progress', function(bytesReceived, bytesExpected) {
//self.emit('progess', bytesReceived, bytesExpected)
var percent = (bytesReceived / bytesExpected * 100) | 0;
process.stdout.write('Uploading: %' + percent + '\r');
})
.on('end', function() {
});
form.parse(req);
the problem is that when i post the image... the image still saving on process.env.TMP my proyect has this dir arq
proyect:
node_modules
uploaded
public
routes
views
app.js
package.json
EDIT 2: PROBLEM IS BODYPARSER
ok from the last code... the problem was the app.js when i configure the app has a bodyParser so that bodyParser method use formidable from the express
the last code is right and functionally, if anybody wanna use the code... just comment the line from express.configure
app.use(express.bodyParser());
to turn off the express bodyparser but express use formidable too so is same thing...
there is another problem
i configure express.bodyParser like this
app.use(express.bodyParser(
{
uploadDir: './uploads',
keepExtensions: true
}));
i change the first code like this
app.post('/register',function(req,res)
{
var oldDir=req.files.img.path;
var newDir='./uploads/'+req.body.email+'/perfil/';
if(req.files)
{
mkdirp(newDir,0777,function(err){
if(err) throw err;
})
}
if(req.files)
{
fs.rename(oldDir,newDir+req.files.img.name,function(err){
if(err) throw err;
});
}
res.send('pow!');
it is functionally and when this code receive a file put the file in the uploads folder next create folder with the name of the email and last move the file into the email folder and rename it like the origin file with the extension
its perfect"!
nooo there is no perfection... i wanna use this methods
.on ('fileBegin', function(name, file){
//rename the incoming file to the file's name
file.path = form.uploadDir + "/" + file.name;
})
.on('progress', function(bytesReceived, bytesExpected) {
//self.emit('progess', bytesReceived, bytesExpected)
var percent = (bytesReceived / bytesExpected * 100) | 0;
process.stdout.write('Uploading: %' + percent + '\r');
})
because from the first code i can configure where put the file from the beginning in the second code i move the file after the server receive the file
i think the method is in express because express use formidable like middleware
how can i configure the method on??
where is the method...?'?
how can i know the methods contained in req.files from the second code?? (because req.files using bodyParser() is same like form.file using formidable in first code)
tnx all
The IncomingForm constructor takes an options object. You'll probably need to use the __dirname global in your path as well.
var form = new formidable.IncomingForm({ uploadDir: __dirname + '/uploaded' });
You can see how the option is applied in the formidable source.
You can keep the renaming code when 'end' is triggered so that it renames after the file is completely uploaded.