代码之家  ›  专栏  ›  技术社区  ›  Dave Miller

react fine uploader S3 upload使用动态文件名

  •  2
  • Dave Miller  · 技术社区  · 7 年前

    出身背景

    var uploader = new qq.s3.FineUploader({
        // ...
        objectProperties: {
            key: function(fileId) {
                return 'folder/within/bucket/' + this.getUuid(fileId);
            }
        }
    });
    

    (摘自 another answer )

    当您想要处理可以从文件/上传器(例如uuid)获得的值以及其他静态信息时,这是很好的。我想要的是包含一个从其他地方传递给这个组件的文件夹名。以下是我迄今为止的尝试:

    //imports...
    const uploader = new FineUploaderS3({
        options: {
            //other options...
            objectProperties: {
                key: function(fileId) {
                    return folderName + '/' + this.getUuid(fileId);
                }
            }
        }
    });
    
    var folderName = null;
    
    class FileUpload extends Component {
        constructor(props) {
            super(props);
    
            folderName = this.props.folderName;
        }
    
        render() {
    
            return (
                <div>
                    <Gallery uploader={uploader} />
                </div>
            );
        }
    };
    
    export default FileUpload;
    

    目前,这会将其添加到s3中一个名为“null”的文件夹中,但我正在props中成功地将folderName传递给这个组件。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jonny Buchanan    7 年前

    它的价值是什么 fileUpload FileUpload 最初是否渲染?如前所述,这只会设置 文件上传 FineUploaderS3 文件上传 componentWillReceiveProps() 要更新 folderName 文件上传 组件,因为最后一个渲染的组件将获胜)。


    class FileUploadComponent extends Component {
      constructor(props) {
        super(props)
        // We can't use an arrow function for the key function below as we need to
        // access both this component's props and the FileUploader instance within it
        let component = this
        this.uploader = new qq.s3.FineUploader({
          // ...
          objectProperties: {
            key(fileId) {
              return `${component.props.folderName}/${this.getUuid(fileId)}`
            }
          }
        })
      }
    
      render() {
        return <div>
          <Gallery uploader={this.uploader}/>
        </div>
      }
    }