transTempFileStream(Datastream, dir, id) { const cloud = require('wx-server-sdk') const fs = require('fs') const path = require('path') // var xlsx = require('node-xlsx'); let fileBase = Datastream[0].fileBase.data; let name = Datastream[0].name; let imgList = ['']; let ext = name.match(/\.[^.]+?$/)[0]; let rd = (Math.random() * (999999 - 100000 + 1) | 0) + 100000; //ccminiHelper.genRandomNum(100000, 999999); fs.writeFileSync(path.join(__dirname, name), fileBase, { encoding: 'base64', mode: 0o777 }); const file_content = fs.createReadStream(path.join(__dirname, name)); cloud.uploadFile({ cloudPath: dir + id + '_' + rd + ext, fileContent: file_content, }).then(res => { imgList[0] = res.fileID; }).catch(error => { console.log(error) }) return imgList; };
云端调用cloud.uploadFile把文件上传到微信小程序官方云存储时,报TypeError从别的接口接收到base64编码的图像,想在云端以base64作为encoding保存为文件后,读取该文件并上传到云存储,在上传时报告了异常: errCode: -1errMsg: "uploadFile:fail TypeError: Cannot destructure property `url` of 'undefined' or 'null'.; at uploadFile api; "相关代码: const fs = require('fs') const path = require('path') const cloud = require('wx-server-sdk') //文件上传函数 function upload_res_file(options) { fs.writeFileSync(path.join(__dirname, 'demo.jpg'), options.fileContent, { encoding: 'base64', mode: 0o777 }); const file_content = fs.createReadStream(path.join(__dirname, 'demo.jpg')); cloud.uploadFile({ cloudPath: options.cloudPath + '.jpg', fileContent: file_content, }).then(options.success).catch(options.fail); }
2023-07-26