How to upload files using nodejs and HAPI?

前端 未结 3 1746
面向向阳花
面向向阳花 2020-12-28 17:44

Can anyone tell me How to upload files Using nodejs and HAPI?

I am getting binary data inside the handler.

Here is my html code:

function sen         


        
3条回答
  •  长发绾君心
    2020-12-28 18:06

    Finally I got the solution to upload the large files using HAPI and Thanks to Roman.

    Here is the solution:

    server.js code

    server.route({
        method: 'POST',
        path: '/api/uploadfiles',
        config: {
              payload:{
                    maxBytes:209715200,
                    output:'stream',
                    parse: false
              }, 
              handler: currentposition.uploadFiles,
        }
    });
    

    Handler code:

    var currentpositionApi = {
    
        fs : require('fs'),
        multiparty: require('multiparty'),
        uploadFiles:function(req,reply){
             var form = new currentpositionApi.multiparty.Form();
                form.parse(req.payload, function(err, fields, files) {
                    currentpositionApi.fs.readFile(files.upload[0].path,function(err,data){
                        var newpath = __dirname + "/"+files.upload[0].originalFilename;
                        currentpositionApi.fs.writeFile(newpath,data,function(err){
                            if(err) console.log(err);
                            else console.log(files)
                        })
                    })
                    console.log(files)
    
                });
    
        }
    }
    

提交回复
热议问题