首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Formidable无法解析大文件

Formidable无法解析大文件
EN

Stack Overflow用户
提问于 2021-07-29 16:32:36
回答 1查看 49关注 0票数 0

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

代码语言:javascript
复制
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" })
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-05 02:23:51

您的返回消息(200).send({“res.status”:“上传文件成功”})太快了,应该在回调中。

这在大文件上可能是有问题的,因为将接收到大文件的开始,然后客户端已经接收到一个响应,该响应可能在逻辑上切断http中的连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68572649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档