We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
用axios.post(),通过formData上传文件,在云函数中无法接收。。 我想在云函数中接收文件,并通过uniCloud.uploadFile(),存储到云存储中去。
The text was updated successfully, but these errors were encountered:
可以试试使用base64传给云函数,云函数用buffer上传空间。 前端使用 FileReader 将文件转为 base64,参考:https://juejin.cn/post/7255785481119727672
const uploadImgToBase64 = (file: File): Promise<string> => { return new Promise((resolve, reject) => { const reader = new FileReader() reader.readAsDataURL(file) reader.onload = () => { resolve(reader.result as string) } reader.onerror = reject }) } const httpRequest = async (file) => { const content = await uploadImgToBase64(file) const res = await axios('/api/upload/upload', { method: 'POST', data: { name: 'test.png', content: content.replace(/.*;base64,/, ''), // 需要去掉文件类型前缀 }, }) return res.data.fileID }
云函数将 Base64 转为 Buffer 并上传阿里云,参考:https://blog.csdn.net/sd21213232/article/details/130545877
// service/upload.js const Service = require('uni-cloud-router').Service module.exports = class MyService extends Service { async upload() { const body = this.ctx.data const { name, content } = body let fileName = '' //截取文件后缀,可能有多个点号 if (name.includes('.')) { fileName = Date.now() + '.' + name.split('.').slice(-1)[0] } else { fileName = Date.now() + '' } let result = await uniCloud.uploadFile({ //保存到云端的文件名 cloudPath: fileName, //base64转buffer(阿里云支持buffer和绝对路径) //event为请求传入的base64数据(不包含文件类型标识) fileContent: Buffer.from(content, 'base64'), }) return { fileID: result.fileID, } } }
限制:阿里云空间云函数 URL 化限制请求体为 1MB,同时 base64 编码后文件体积会增大 1/3,我在项目中限制为 720KB 了。
Sorry, something went wrong.
No branches or pull requests
用axios.post(),通过formData上传文件,在云函数中无法接收。。
我想在云函数中接收文件,并通过uniCloud.uploadFile(),存储到云存储中去。
The text was updated successfully, but these errors were encountered: