Search code examples
fileserveruploadhapi

Uploading file on Hapi.js server


I'm trying to upload file on a hapi server, and my goal would be to upload a zip file and unzip it on the server, but I currently have problems withs the upload part for a single file...

my frontend is made with react and I'm selecting a file with a <input>

my route is made like this

        method: 'POST',
        path: '/upload',
        config: {
            payload: {
                maxBytes: 209715200,
                output: 'stream',
                parse: true
            },          
            handler: handlers.uploadFile,
            description: 'upload file'
        }
    });

I used a stream type output, but I can't figure out what is the type to used depending on the situation between stream, data, or file.

Here my handler uploadfile() is this one :

    handler.uploadFile = async (req, h) => {
        var doc = req.payload
    return true;
    }

But I can't get any informations on my file like doc.name or doc._hapi.name so I don't really know how to use the data here.

If someone know a site where all of this is explain, or could help me on that that would help a lot !

Thanks !


Solution

  • Ok so I just figure out what the problem was.

    I needed to pass my data as a formData from my react component like that

    var formData = new FormData();
    formData.append("file", data)
    

    if anyone have the same problem, that is a solution I suppose