Javascript 使用Post Form et发送图像,并使用节点Js将其编码为based64

Javascript 使用Post Form et发送图像,并使用节点Js将其编码为based64,javascript,node.js,mongodb,forms,post,Javascript,Node.js,Mongodb,Forms,Post,我尝试从post表单(我网站上的用户)中获取一个图像,并将其编码为base64以记录在我的数据库中,我尝试了,但当我获取文件时,这些文件只取名称。。(“5165151.jpg”) 我的代码怎么了 app.post("/postCreas",(req,res)=>{ const imgUp = new imgModel({ }) let title = req.body.title let commentaire = req.body.s

我尝试从post表单(我网站上的用户)中获取一个图像,并将其编码为base64以记录在我的数据库中,我尝试了,但当我获取文件时,这些文件只取名称。。(“5165151.jpg”) 我的代码怎么了

app.post("/postCreas",(req,res)=>{
    const imgUp = new imgModel({

    })
    let title = req.body.title
    let commentaire = req.body.sujet
    let img = req.body.upload
    let postCollection = db.collection('POSTCreas')
   /* let im64= Buffer.from(img, 'binary').toString('base64')   */ 
    console.log("test :"+img) 
        
    
    
    
    postCollection.insertOne({_id:title,title:title, commentaire : [commentaire],fichiers:img})        
    res.redirect("vosCreas")                       
    res.end()    
})
我的表格代码是:

  app.post("/postAstuces",(req,res)=>{
    let title = req.fields.title
    let commentaire = req.fields.sujet
    let img = req.files.upload.toString('base64')
    let postCollection = db.collection('POSTastuces')
    console.log("test : "+img)
    
    
    postCollection.insertOne({_id:title,title:title, commentaire : [commentaire],fichiers:img})        
    res.redirect("vosAstuces")                       
    res.end()    
})

祝你有愉快的一天

正如所怀疑的,您的
标记中缺少文件上载所需的
enctype=“multipart/form data”


您还需要确保服务器知道如何解析多部分表单数据。如何做到这一点取决于您的配置方式。

您还需要显示表单代码。很可能您缺少正确的
enctype
。当然,很抱歉。好的,我添加了您的建议,例如:但是现在,当我从帖子结果中尝试console.log时,我的所有输入都为空…请参阅我答案的第二部分。您需要确保您的服务器知道如何解析多部分表单数据。好的,我获取字符串(标题、注释),然后获取图像并转换为base64,但这给了我:���rӛ��- 对于每一张照片。。这是正常的吗?这看起来像是二进制数据的字符串表示,所以是的,它可能是正确的(在您将其编码为base64之前)。好的,但我认为它是在编码之后。。我用新参数更新代码