Javascript 使用multer快速上载文件不使用urlencodedParser

Javascript 使用multer快速上载文件不使用urlencodedParser,javascript,node.js,express,http-post,multer,Javascript,Node.js,Express,Http Post,Multer,我正在尝试上传一个带有post请求表单的图像。在我的express.js服务器中,我同时使用multer和urlencodedParser,因为我将所有html输入作为json对象传递给post请求主体和一个文件选择,我将使用multer处理它 以下是表单的HTML代码: <form action="http://localhost:8000/api/addOrgs" method="post"> <div class="

我正在尝试上传一个带有post请求表单的图像。在我的express.js服务器中,我同时使用multer和urlencodedParser,因为我将所有html输入作为json对象传递给post请求主体和一个文件选择,我将使用multer处理它

以下是表单的HTML代码:

<form action="http://localhost:8000/api/addOrgs" method="post">
    <div class="form-row">
        <!-- OTHER CODE -->
            <div class="form-group col-md-6">
                <label>Image or Logo</label>
                <input type="file" class="form-control" name="image" id="fileToUpload">
            </div>
        <button class="btn btn-primary">  Add the organization to the system</button>
    </div>
</form>
以下是express post请求处理程序:

// to add a new organization to the list
app.post('/api/addOrgs', upload.single('imageToUpload'), urlencodedParser, (req, res)=> {
    try {
        var newobj = req.body;

        // want to change name of id in obj
        // add image name and delete image field
        newobj['img'] = newobj.image;
        delete newobj.image;

        // read orgs file
        let orgs = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../orgs.json')));
        
        //... OTHER CODE
        // send respond
        res.send('<h1>Successfully added!</h1><br><p>'+JSON.stringify(newobj, null, 4)+'</p></br><b>Now you can go <a href="/good">BACK</a></b>')
       // OTHER CODE
    } catch (e) {
        // ... OTHER CODE
    }
});
//向列表中添加新组织的步骤
app.post('/api/addOrgs',upload.single('imageToUpload'),urlencodedParser,(req,res)=>{
试一试{
var newobj=请求主体;
//要更改obj中id的名称吗
//添加图像名称和删除图像字段
newobj['img']=newobj.image;
删除newobj.image;
//读取orgs文件
让orgs=JSON.parse(fs.readFileSync(path.resolve(_dirname,../orgs.JSON'));
//…其他代码
//发送回复
res.send('Successfully added!
'+JSON.stringify(newobj,null,4)+'


现在您可以开始了') //其他代码 }捕获(e){ //…其他代码 } });
问题是我成功地将文件名上传到通过req.body发送的json对象中,但它没有将文件保存在文件夹中。问题是否来自对
app.post()
参数使用
urlencodedParser


或者我必须做些什么才能将文件保存在我的
app.post()
函数中?

文件的表单应该具有
enctype=“multipart/form data”
属性。试试这个,它应该可以工作

,所以我不得不像你说的那样修改html表单
,然后在函数
upload.single('imageToUpload')
中出现命名问题,并且必须重写为
upload.single('image')
,因为输入标记有
name=“image”
。是的,这也是必须的。。不管怎样,很高兴听到您的代码已经启动并再次运行!
// to add a new organization to the list
app.post('/api/addOrgs', upload.single('imageToUpload'), urlencodedParser, (req, res)=> {
    try {
        var newobj = req.body;

        // want to change name of id in obj
        // add image name and delete image field
        newobj['img'] = newobj.image;
        delete newobj.image;

        // read orgs file
        let orgs = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../orgs.json')));
        
        //... OTHER CODE
        // send respond
        res.send('<h1>Successfully added!</h1><br><p>'+JSON.stringify(newobj, null, 4)+'</p></br><b>Now you can go <a href="/good">BACK</a></b>')
       // OTHER CODE
    } catch (e) {
        // ... OTHER CODE
    }
});