使用云开发怎么获取小程序码
发布于 6 年前 作者 chao63 19189 次浏览 来自 问答

运用request获取小程序码二进制流后,想要存储到云开发的文件管理,但一直提示文件路径是read-only file system,有哪位大哥知道咋整吗

return new Promise((resolve, reject) => {
  let stream = request({
    url: url,
    method: "POST",
    headers: {
      "content-type": "application/json",
      "content-Length": JSON.stringify(data).length
    },
 
    body: json
     
  })
    .pipe(fs.createWriteStream(path.join(__dirname, 'demo.png')))
  stream.on('finish', function () {
    cloud.uploadFile({
      cloudPath: 'demo.png',
      fileContent: '/var/user/demo.png',
    })
 
  })
3 回复

var rp = require('request-promise');//还需要npm install request


 


//以下代码写在云函数里
async function request(url) {
    return new Promise(function (resolve) {
      rp(url)
        .then(function (data) {
          resolve(data);
        })
        .catch(function () {
          resolve({ err: 1 });
        });
    });
  }
 
  var createOpt = function (token, scene, page) {
    var options = {
      method: 'POST',
      uri: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token,
      encoding: null, //一定要加这个!!!!,不然存进去乱码,试了好久才发现是这里的问题,发出来给大家节约时间
      body: {
        "scene": scene,
        "page": page
      },
      json: true // Automatically stringifies the body to JSON
    };
    return options
  }
 
 
    var options = createOpt('access_token自己获取', 'test=key&ffff=lalalla', 'pages/index/index')
    var fileStream = await request(options)
 
    return await cloud.uploadFile({
      cloudPath: 'testCode3.png',
      fileContent: fileStream,
    })

我解决了,文件存在 /tmp 目录下就ok,文档里面有说明的

您好 请问解决了嘛

回到顶部