强大的 returns TypeError,ERR_INVALID_ARG_TYPE:"path" 参数未定义
Formidable returns TypeError, ERR_INVALID_ARG_TYPE: The "path" argument is undefined
我在使用 formidable 上传文件时遇到问题。我在 Windows 服务器 2016.
我的代码(完整如下所示)基于 https://www.geeksforgeeks.org/how-to-upload-file-using-formidable-module-in-node-js/
const express = require('express');
const fs = require('fs');
const path = require('path')
const formidable = require('formidable');
const app = express();
app.post('/api/upload', (req, res, next) => {
const form = new formidable.IncomingForm(
{ uploadDir: __dirname + '\tmp', keepExtensions: true }
);
form.parse(req, function(err, fields, files){
var oldPath = files.profilePic.path;
var newPath = path.join(__dirname, 'uploads')
+ '/'+files.profilePic.name
var rawData = fs.readFileSync(oldPath)
fs.writeFile(newPath, rawData, function(err){
if(err) console.log(err)
return res.send("Successfully uploaded")
})
})
});
app.listen(3000, function(err){
if(err) console.log(err)
console.log('Server listening on Port 3000');
});
我使用 Postman 发送文件。
触发/api/upload
API时,发送的文件正确放置在tmp文件夹中,但读取路径时出现问题:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
此消息指向 fs.readFileSync(oldPath)
。
console.log('files='+files)
returns files=[object Object]
console.log('files.profilePic='+files.profilePic)
returns
C:\somepath\node_modules\formidable\src\PersistentFile.js:50
return `PersistentFile: ${this._file.newFilename}, Original: ${this._file.originalFilename}, Path: ${this._file.filepath}`;
^
TypeError: Cannot read properties of undefined (reading 'newFilename')
at PersistentFile.toString (C:\somepath\node_modules\formidable\src\PersistentFile.js:50:42)
所有 4 个引用模块都存在于 node_modules 文件夹中。
简单改变
var oldPath = files.profilePic.path;
至
var oldPath = files.profilePic.filepath;
还要确保您创建了“上传”文件夹,因为代码不会创建它,如果没有它,它将失败。
编辑:附带说明是,如果您的环境只是在控制台记录一个对象时吐出 [object object],那么可能会得到一个提供有用信息的新环境(visual studio 代码很好) .
我在使用 formidable 上传文件时遇到问题。我在 Windows 服务器 2016.
我的代码(完整如下所示)基于 https://www.geeksforgeeks.org/how-to-upload-file-using-formidable-module-in-node-js/
const express = require('express');
const fs = require('fs');
const path = require('path')
const formidable = require('formidable');
const app = express();
app.post('/api/upload', (req, res, next) => {
const form = new formidable.IncomingForm(
{ uploadDir: __dirname + '\tmp', keepExtensions: true }
);
form.parse(req, function(err, fields, files){
var oldPath = files.profilePic.path;
var newPath = path.join(__dirname, 'uploads')
+ '/'+files.profilePic.name
var rawData = fs.readFileSync(oldPath)
fs.writeFile(newPath, rawData, function(err){
if(err) console.log(err)
return res.send("Successfully uploaded")
})
})
});
app.listen(3000, function(err){
if(err) console.log(err)
console.log('Server listening on Port 3000');
});
我使用 Postman 发送文件。
触发/api/upload
API时,发送的文件正确放置在tmp文件夹中,但读取路径时出现问题:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
此消息指向 fs.readFileSync(oldPath)
。
console.log('files='+files)
returns files=[object Object]
console.log('files.profilePic='+files.profilePic)
returns
C:\somepath\node_modules\formidable\src\PersistentFile.js:50
return `PersistentFile: ${this._file.newFilename}, Original: ${this._file.originalFilename}, Path: ${this._file.filepath}`;
^
TypeError: Cannot read properties of undefined (reading 'newFilename')
at PersistentFile.toString (C:\somepath\node_modules\formidable\src\PersistentFile.js:50:42)
所有 4 个引用模块都存在于 node_modules 文件夹中。
简单改变
var oldPath = files.profilePic.path;
至
var oldPath = files.profilePic.filepath;
还要确保您创建了“上传”文件夹,因为代码不会创建它,如果没有它,它将失败。
编辑:附带说明是,如果您的环境只是在控制台记录一个对象时吐出 [object object],那么可能会得到一个提供有用信息的新环境(visual studio 代码很好) .