强大的无法解析大文件

Formidable couldn't parse large file

以下代码适用于小文件,但可以上传超过 50kb 的文件。我认为我应该使用 maxFileSize 进行设置。我在客户端使用 Uppy。在服务器中 console.log 之后,实际上它确实执行了请求。我在 Whosebug 中没有得到任何线索,真的需要帮助。

const upload = (req, res) => {
    // formidable : to parse html form data
    const form = new formidable.IncomingForm({ multiples: true, maxFileSize: 10000 * 1024 * 1024 })
    const d = new Date();
    // I have console.log here and everything seems fine
    form.parse(req, (err, fields, files) => {
        console.log('err', err)      // returns nothing
        console.log('files', files) // returns nothing
        console.log('fields', fields) // returns nothing
        if (err) {
            console.log("Error parsing the files");
            console.log(err);
            return res.status(400).json({
                message: "There was an error parsing the files",
                status: "Fail",
                error: err
            })
        }
        for (let file in files) {
            try {
                if (files[file]) {
                    let oldPath = files[file]['path']
                    let rawData = fs.readFileSync(oldPath)

                    const month = parseInt(d.getMonth() + 1) < 10 ? '0' + parseInt(d.getMonth() + 1) : parseInt(d.getMonth() + 1)

                    let today = `${d.getFullYear()}_${month}_${d.getDate()}`
                    let folderPath = __basedir + `\media\uploads\storage\${today}\`;
                    // folderPath = ..\dashboard-v2.0\server\media\uploads\storage\2021_06_18\

                    if (!fs.existsSync(folderPath)) {
                        fs.mkdirSync(folderPath, {
                            recursive: true
                        });
                    }

                    // newPath =..\dashboard-v2.0\server\media\uploads\storage\2021_06_18\WIN.jpg
                    let newPath = folderPath + files[file]['name']
                    let databasePath = `storage/${today}/${files[file]['name']}`;
                    let filename =  files[file]['name'] // example_files.zip

                    if (fs.existsSync(newPath)){
                        // if file is existed then add Date.now()
                        let time =  Date.now()
                        let filenameSplit = filename.split('.')
                        
                        filename = filenameSplit[0] + '_' + time + '.' + filenameSplit[1] 
                        // filename = WIN_1626750408096.jpg

                        newPath = folderPath + filename
                        databasePath = `storage/${today}/${filename}`;
                    }
                    
                    fs.writeFile(newPath, rawData, async (err) => {
                        if (err) {
                            console.log(err);
                            return res.status(400).send({ "err": err })
                        }

                        const userToken = jwt.verify(fields.user, config.TOKEN_SECRET)

                        const newFiles = {
                            filename: filename,
                            user_id: ObjectId(userToken.id),
                            filepath: databasePath,
                            added_time: Date.now(),
                        }

                        const result = await db.collection("ate_files").insertOne(newFiles)
                        console.log(`Created with the following id: ${result.insertedId}`)

                        console.log(`Successfull upload ${newPath}`);
                    })
                }
            } catch (err) {
                console.log(`Error: ${err}`);
                return res.status(409).send({ "error": `${err}` })
            }
        }
        
    })

    return res.status(200).send({ "message": "Successfully uploadded the files" })
}

你的return res.status(200).send({ "message": "Successfully uploaded the files" }) 太快了,应该在回调中。

大文件可能会出现问题,因为会收到大文件的开头,然后客户端已经收到一个响应,这在逻辑上可以切断 http

中的连接