【问题标题】:Trying to get file preview thumbnail to render试图让文件预览缩略图渲染
【发布时间】:2018-01-10 18:44:28
【问题描述】:

只是想用onDrop() 来渲染预览缩略图。我在几个网站上看到了一些你放置文档的网站,它显示了第一页的缩略图。我要么得到一个损坏的链接图像,要么根本没有图像。

这是我作为参考使用的,虽然官方文档并没有太大帮助:

https://react-dropzone.js.org/

React-Dropzone image preview not showing

https://medium.com/technoetics/handling-file-upload-in-reactjs-b9b95068f6b

这是我目前使用的代码。这不会呈现缩略图,但“提交”和“取消”按钮会像有东西一样向下移动,但我什么也没看到。

import _ from 'lodash';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { submitDocument } from '../../actions/documents';
import Dropzone from 'react-dropzone';

class SubmitDocuments extends Component {

    constructor() {
        super();
        this.state = {
            filesToBeSent: [],
            filesPreview: [],
        }

        this.handleClick = this.handleClick.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.onDrop = this.onDrop.bind(this);
    }

    handleClick(event) {
        this.setState({
            filesToBeSent: [],
            filesPreview: [],
        });
    }

    handleSubmit(event) {
        event.preventDefault();
        this.props.submitDocument(this.state.filesToBeSent);
    }

    onDrop(acceptedFiles) {
        console.log(acceptedFiles);
        var filesToBeSent = this.state.filesToBeSent;       
        _.map(acceptedFiles, f => {
            filesToBeSent.unshift(f);
        });

        filesToBeSent = _.uniqBy(filesToBeSent, 'name');

        var filesPreview = [];

        _.map(filesToBeSent, i => {
            filesPreview.unshift(
                <div key={i.name}>
                    {/*<h5>{i.name} - {i.size} bytes</h5>*/}
                    <img src={window.URL.revokeObjectURL(i.preview)} />
                </div>
            )            
        });

        this.setState({
            filesToBeSent,
            filesPreview
        });
    }

    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <div className='panel panel-default'>
                    <div className='panel-heading'>
                        <h4><strong>Submit Documents</strong></h4>
                    </div>

                    <div className='panel-body'>
                        <Dropzone className='dropzone' onDrop={this.onDrop}> 
                            <h3>Click to add files or drag files here to upload</h3>
                        </Dropzone>
                        {this.state.filesPreview}
                        <button type='submit' disabled={this.state.filesPreview.length < 1} className='btn btn-primary'>Submit</button>
                        <button type='button' className='btn btn-danger' onClick={this.handleClick}>Cancel</button>
                    </div>
                </div>
            </form>
        ); 
    }
}

function mapStateToProps(state) {
    return {
        survey_id: state.documents.survey_id
    }
}

export default connect(mapStateToProps, { submitDocument })(SubmitDocuments);

现在更改为以下导致损坏的图像图标:

<img src={i.preview} />

不过上传正常。

我在这里做错了什么?我觉得我对preview 的解释可能与文档的含义不同,或者它只适用于图像文件,而我的应该适用于.pdf, .xlsx., .txt

编辑

这就是console.log(filesToBeSent) 的样子。

这是ilodash _.map(filesToBeSent, i =&gt; {} 之后的样子。

【问题讨论】:

    标签: reactjs react-redux react-dropzone


    【解决方案1】:

    这是因为数据以 base64 编码字符串的形式返回,而不是其本地存储位置的“url”。使用这个

    <img alt="Embedded Image" src={`data:image/png;base64,${i.preview}`} />
    

    请注意,我已将 src 设为 data:image/png;base64,(必须包含逗号)

    【讨论】:

    • 感谢您的反馈。它现在正在生成 net::ERR_INVALID_URL 错误。 i.preview 的 URL 的格式为:blob:http://localhost:8000/a85a4fa8-2cf5-4c84-b939-c7609a4b9811。正在研究解决该问题。
    • 抱歉。我实际上并没有将“预览”与放置区一起使用。我只是使用你所说的filesToBeSent,它实际上应该是数据字符串。在您的情况下,它是一个数组,但只需提供索引。
    • 听起来我应该只是指i,它应该是:{&lt;img src={data:image/png;base64,${i}} /&gt;}。但是,这会导致相同的错误。我修改了我的问题以显示我所看到的屏幕截图。 i 只会引用 filesToBeSent 中的索引(但我可能是错的)。
    • 从你的截图来看,第一个是未定义的......第二个显示你试图将整个对象传递给 src 注意它说data:image/png;base64,[object File]
    • 您可以在预览中向 URL 发出 XMLHTTPRequest(或获取)并将响应类型设置为 blob 以返回,但这很愚蠢。我以前使用过 React Dropzone,但我总是将它与文件阅读器结合使用。
    【解决方案2】:

    事实证明,我需要这样的服务来完成我想要完成的事情:

    FilePreviews.io

    【讨论】:

      猜你喜欢
      • 2014-12-04
      • 2015-06-07
      • 2017-11-17
      • 2011-12-19
      • 1970-01-01
      • 2016-12-17
      • 2012-03-12
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多