首页 > 解决方案 > URIError:无法解码参数 - 编码二进制图像 Filepond

问题描述

我将图像作为 type:binary 发送并解析为 JSON 到数据库,其中它是一个 json 字符串。如果我想查询图像,它会引发错误

URIError:无法在 decodeURIComponent () 处解码参数“%EF%BF%BD...”

我按如下方式保存对图像进行编码:在模型文件夹中:

...
paintings: { type: Buffer },
paintingsType:{ type: String }

    postSchema.virtual('paintingsP').get(function() {
        if (this.paintings != null && this.paintingsType != null) {
            return `data:${this.profilpicType};charset=utf-8;base64,${this.profilpic.toString('base64')}`
        }
    })

在 router.js 内部

// **Inside a router.put function**
...
if (req.body.paintings != null && req.body.paintings !== '') {
            savePaintings(post, req.body.paintings)
        }

function savePaintings(post, profilpictureEncoded) {
     if (profilpictureEncoded == null) return;
     const profpic = JSON.parse(profilpictureEncoded);
     if (profpic != null && imageMimeTypes.includes(profpic.type)) { // If the file is a json obj & from the type image (jpg, png)
         post.paintings = new Buffer.from(profpic.data, 'base64')    // Buffer.from(where, how)
         post.paintingsType = profpic.type
     }
 }
await post.save();

如果我查找所有接缝都很好,那么问题可能出在哪里?我的要求有错吗?数据库内部:D B

我使用 express、mongoose、ejs 和 FilePond (用于保存文件)

标签: node.jsjsondatabasemongodbfilepond

解决方案


推荐阅读