小程序上传图片文件到云函数中做图片安全检验,带上编码后违禁图片也会返回成功,怎么破?
发布于 6 年前 作者 xiejing 14278 次浏览 来自 问答

小程序端使用fileManagerreadFileSync(‘文件路径’,‘某种编码’)读取本地图片文件传到云函数检测文件安全,带上编码的话读取的是乱码以至于到云函数端全部返回通过,不带参数读取到的是二进制文件内容违禁图片不能通过。

//让用户选择或拍摄照片
        wx.chooseImage({
            count: remainCount,
            sizeType: ['original', 'compressed'],
            sourceType: ['album'],
            success(res) {
                console.log(res)
                const tempFiles = res.tempFiles
                //选择完成会先返回一个临时地址保存备用
    let imgs = that.data.imgs
const fileManager = wx.getFileSystemManager()
                    for (let index = ; index < tempFiles.length; index++) {
                    wx.showLoading({
                        title: '加载图片...',
                        mask: true
                    });
                    // 如果文件大于1M
if (tempFiles[index].size > 1024 * 1000) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '大于1M的图片已被去除',
                            icon: 'none',
                        });
                        continue
                    }
                    const file = tempFiles[index].path;
                    console.log('file', file)

                    try {
                          var imgBuffer = fileManager.readFileSync(file)//*******注意,是这里,带上编码返回的就是乱码字符串,传到云函数就会全部通过,如果不带编码,安全检测违禁图片就不能通过。但是不带编码有些机器读取会非常的慢,所以有什么方法可以既能在小程序端读取流畅,又能准确的检测到违禁图片??
console.log('fileData', imgBuffer)
  
  
                          // 图片安全检测
                          wx.cloud.callFunction({
                            name: 'checkImage',
                            data: {
                                imgBuffer: imgBuffer
                            }
                        }).then(res => {
                            wx.hideLoading();
                            console.log('检测结果', res)
                        })
                    } catch (e) {
                        console.log('e', e)
                    }

                }

            }
        })
回到顶部