开发者社区> 问答> 正文

EMAS Serverless文件存储上传文件,怎样制定上传文件到特定目录,

云端已经支持,但是小程序端 api文档并没有更新。

app.mpServerless.file.uploadFile(options)

    const options = {
        filePath: this.data.avatarUrl,
        cloudPath:this.data.userId,
        cloudPathAsRealPath:true,
        headers: {
          contentDisposition: 'header',
        },
      };
      console.log('formSubmit:', options)
    app.mpServerless.file.uploadFile(options).then((image) => {
        console.log('ret', image)
        // that.setData({
        //   iconUrl: image.fileUrl,
        // });
      }).catch(console.error);

展开
收起
baobeer 2023-08-20 13:07:52 45 0
1 条回答
写回答
取消 提交回答
  • 根据您提供的代码示例,EMAS Serverless文件存储的uploadFile方法用于上传文件。要将文件上传到特定目录,可以通过修改cloudPath参数来指定目录路径。

    下面是一个示例:

    const options = {
    filePath: this.data.avatarUrl, // 本地文件路径
    cloudPath: 'custom/directory/filename.jpg', // 指定云端目录路径和文件名
    cloudPathAsRealPath: true,
    headers: {
    contentDisposition: 'header',
    },
    };

    app.mpServerless.file.uploadFile(options)
    .then((image) => {
    console.log('上传成功', image);
    // 处理上传成功后的逻辑
    })
    .catch((error) => {
    console.error('上传失败', error);
    // 处理上传失败后的逻辑
    });

    在上述代码中,将cloudPath参数设置为custom/directory/filename.jpg,即可指定文件上传到custom/directory目录下,并命名为filename.jpg

    请注意,在使用自定义目录时,确保目录已经在云端创建。如果目录不存在,可以先通过其他方式(例如云端管理界面)创建目录,然后再执行文件上传操作。

    2023-08-26 17:39:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
All in Serverless 阿里云核心产品全面升级 立即下载
AIGC 浪潮之上,森马的 Serverless 实践之旅 立即下载
极氪大数据 Serverless 应用实践 立即下载