【问题标题】:How do I send progress of S3 upload in aws-amplify (React native)?如何在 aws-amplify (React native) 中发送 S3 上传进度?
【发布时间】:2019-03-09 08:53:56
【问题描述】:

我能够将图像上传到 S3 并将进度发送到控制台,但无法设置组件的状态。似乎无法将进度发送到外部函数。即使我有一个将其记录到控制台的函数,该函数也不会被调用。

Storage.put(text, Buffer.from(photo.base64, "base64"), {
  progressCallback(progress) {
    prog = parseInt(progress.loaded/progress.total*100)
    console.log(prog+"%");
    this.setState({uploadProgress: prog+"%"})
  },
  contentType: "image/jpeg"
})

【问题讨论】:

    标签: react-native amazon-s3 aws-amplify


    【解决方案1】:

    这是因为您在 Storage.put 方法的回调中调用了this。您可以在 Storage.put 方法之外缓存对 this 的引用,如下所示(因为没有更好的术语):

    const foo = this;
    Storage.put(text, Buffer.from(photo.base64, "base64"), {
      progressCallback(progress) {
        prog = parseInt(progress.loaded/progress.total*100)
        console.log(prog+"%");
        foo.setState({uploadProgress: prog+"%"})
      },
      contentType: "image/jpeg"
    })
    

    【讨论】:

    • 谢谢!经过一天尝试各种取消引用/传递,这是一个有效的方法......
    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多