Search code examples
javascriptajaxnode.jsmultipartform-dataformidable

upload files with ajax and node.js without express


Hello guys i'm trying send files with ajax and node but i don't find the info that show me how do!!, i'm now built a script but me can follow more

var server=http.createServer(function(req, res){
    if(req.url==='/upload'){
        if(req.method==='OPTIONS'){//OPTIONS is the method that show in the server when i send files
            req.on('data', function(a){
                console.log(a);//i dont know what are?? after to here!!
            });
        }
    }
});

i have listened of some module like formidable, multipart, and busboy but i can't run its

Thanks

i can solved it with FORMIDABLE :) look the next code:

var server=http.createServer(function(req, res){
    if(req.url==='/upload'){
        var headers = {};
              // IE8 does not allow domains to be specified, just the *
              // headers["Access-Control-Allow-Origin"] = req.headers.origin;
              headers["Access-Control-Allow-Origin"] = "*";
              headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
              headers["Access-Control-Allow-Credentials"] = false;
              headers["Access-Control-Max-Age"] = '86400'; // 24 hours
              headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
        if(req.method==='OPTIONS'){
              res.writeHead(200, headers);
              res.end();
        }else if(req.method==='POST'){
            var form = new formidable.IncomingForm(),
                files = [],
                fields = [];
            form.uploadDir = '/home/sonick7/';//direccion donde va a ser gusradado

            form.on('field', function(field, value) {
                fields.push([field, value]);
              })
              .on('file', function(field, file) {
                console.log(file.name, file.size, file.type, file.path)
                files.push([field, file]);
              })
              .on('end', function() {
                console.log('Upload terminado ');
                res.writeHead(200, headers);
                res.end();
              });
            form.parse(req);//no se que hace eso y para que sirve el modulo util?
        }
    }
});

Solution

  • UPDATE AND SOLVED

    let server=http.createServer((req, res)=>{
        if(req.url==='/upload'){
            let headers = {}
                  // IE8 does not allow domains to be specified, just the *
                headers={
                    'Access-Control-Allow-Origin':'*',
                    'Access-Control-Allow-Methods':'POST, GET, PUT, DELETE, OPTIONS',
                    'Access-Control-Allow-Credentials':false,
                    'Access-Control-Max-Age':'86400', // 24 hours
                    'Access-Control-Allow-Headers':'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
                  }
            if(req.method==='OPTIONS'){
                  res.writeHead(200, headers)
                  res.end()
            }else if(req.method==='POST'){
                let form=new formidable.IncomingForm(),
                    files=[],
                    fields=[]
                form.uploadDir='/home/sonick7/'
    
                form.on('field', (field, value)=>{
                    fields.push([field, value])
                })
                .on('file', (field, file)=>{
                    files.push([field, file])
                })
                .on('end', ()=>{
                    console.log(files, fields, 'FT here awefwa', __filename)
                    console.log('Upload terminado ')
                    res.writeHead(200, headers)
                    res.end()
                })
                form.parse(req)
            }
        }
    })