anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 190 forks source link

cannot find appended formdata in request body #102

Closed daton89 closed 6 years ago

daton89 commented 6 years ago

i'm appending formdata fields to build the key, my client side function is:

function startUplaod() {
        const url = '/api/v1/files'
        const data = new FormData()

        values.files.forEach(file => {
            data.append(file.name, file)
        })

        data.append('_id', values._id)
        data.append('save_in', 'document')

        const config = {
                         // i've tried to set multipart form data but nothing changed
            // headers: {
            //  'content-type': 'multipart/form-data'
            // },
            onUploadProgress: function(progressEvent) {
                var percentCompleted = Math.round(
                    (progressEvent.loaded * 100) / progressEvent.total
                )
                console.log('percentCompleted =>', percentCompleted)
            }
        }
        Axios.post(url, data, config)
            .then(res => {
                console.log('res =>', res)
            })
            .catch(error => {
                console.log('error upload', error)
            })
    }

my server route:

const upload = multer({
            storage: multerS3({
                s3: s3,
                bucket: config.spaces.Bucket,
                acl: 'private',
                key: keyCb,
                metadata: createMetadata(req.user)
            })
        }).any()

    function keyCb(request, metaFile, cb) {

            const { save_in, _id } = request.body

            console.log('req.body =>', save_in, _id)

            if (!save_in || !_id)
                return cb(new Error('Required id and saveIn not found'))

            const key = save_in + _id + metaFile.originalname

            cb(null, Key)
        }

how can i find the data appended to the formData?

daton89 commented 6 years ago

reading the source code of dropzone.js i've figured out that some servers like S3 expect the file to be the last parameter

so i've fixed appending other data before append the file.