uni-app uniCloud.uploadFile接口传buffer报错 ValidationFailed:参数校验错误 Invalid options.filePath
uni-app uniCloud.uploadFile接口传buffer报错 ValidationFailed:参数校验错误 Invalid options.filePath
操作步骤:
- 1
预期结果:
- 1
实际结果:
- 1
bug描述:
更多关于uni-app uniCloud.uploadFile接口传buffer报错 ValidationFailed:参数校验错误 Invalid options.filePath的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你这fileContent是从客户端传给云函数的?Buffer不是合法的json value 序列化的时候类型就没了变成了普通Object。如果你一定要这么用建议使用base64,云端再把base64转成buffer
更多关于uni-app uniCloud.uploadFile接口传buffer报错 ValidationFailed:参数校验错误 Invalid options.filePath的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在使用 uniCloud.uploadFile
接口时,如果你传递的是 Buffer
数据,而出现了 ValidationFailed: 参数校验错误 Invalid options.filePath
的错误提示,通常是因为 uploadFile
接口期望的是一个文件的路径(filePath
),而不是直接传递 Buffer
数据。
uniCloud.uploadFile
的 filePath
参数通常是一个文件的临时路径或本地路径,而不是直接的二进制数据。如果你需要上传 Buffer
数据,你需要先将 Buffer
数据保存为临时文件,然后使用该文件的路径来调用 uploadFile
。
以下是一个示例代码,展示如何将 Buffer
数据保存为临时文件并上传:
const fs = require('fs');
const path = require('path');
// 假设你有一个 Buffer 数据
const buffer = Buffer.from('Hello, World!');
// 将 Buffer 数据保存为临时文件
const tempFilePath = path.join(__dirname, 'tempFile.txt');
fs.writeFileSync(tempFilePath, buffer);
// 使用 uniCloud.uploadFile 上传文件
uniCloud.uploadFile({
filePath: tempFilePath,
cloudPath: 'example.txt', // 上传到云存储的路径
success: (res) => {
console.log('文件上传成功', res);
// 上传成功后删除临时文件
fs.unlinkSync(tempFilePath);
},
fail: (err) => {
console.error('文件上传失败', err);
// 上传失败后删除临时文件
fs.unlinkSync(tempFilePath);
}
});