javascript - 图像预览在 react 中不起作用

标签 javascript image reactjs onclick onchange

我正在尝试以下图像预览操作。 每当调用 onChange 函数 ImagePreview 时,更改函数 都不会响应。运行 console.log("outside readURL") 后卡住了。我不明白为什么会发生这种情况。 有什么帮助吗?

import React, { Component } from 'react'; 

class ImagePreview extends Component {
   constructor(props) {
     super(props);
     this.state = { images_attributes: [], imgSrc:'' };
   }
   fileUpload() {
     $("#new_post_image").click();
   }
   imageChange() {
    function readURL(input) {
      console.log("bla");
      var file = this.state.images_attributes[0].files[0];
      if(this.state.images_attributes[0].files[0]) {
        var reader = new FileReader();
        // var url = reader.readAsDataURL(file.name);

        console.log(file);

        // console.log(reader);

        reader.onloadend = () => {
          console.log(reader);
          console.log(reader.result);
        }
      }
    }

    console.log("outside readURL");
    $("#new_post_image").change(function() {
      console.log("above readURL");
      readURL(this);
    });

    // if(file.name) {
    //   this.setState({
    //       imgSrc: file.name
    //   })
    // }
  }
   render() {
     <form>
       <img src={this.state.imgSrc} />
       <input id="new_post_image" className="file-image" type="file" name="image" accept="image/*" onChange={this.imageChange.bind(this)} ref={(input) => {this.state.images_attributes[0] = input}} />
       <button type="button" className="camera-button" onClick={this.fileUpload.bind(this)}>
         <i className="fas fa-camera"></i>
         <label className="camera-text"><h4 className="camera-text">Image</h4></label>
       </button>
     </form>
   }
}
export default ImagePreview;

最佳答案

使用此代码并尝试:

imageChange(e) {
   e.preventDefault(); 
   let reader = new FileReader();
   let file = e.target.files[0];

   reader.onloadend = () => {
     this.setState({
       file: file,
       imagePreviewUrl: reader.result
     });
   }

  reader.readAsDataURL(file)
 }

替换渲染方法:

render() {
 <form>
   <img src={this.state.imagePreviewUrl} /> // in constructor don't initilazie with array instaed with blank string
   <input id="new_post_image" className="file-image" type="file" name="image" accept="image/*" onChange={this.imageChange.bind(this)} ref={(input) => {this.state.images_attributes[0] = input}} />
   <button type="button" className="camera-button" onClick={this.fileUpload.bind(this)}>
     <i className="fas fa-camera"></i>
     <label className="camera-text"><h4 className="camera-text">Image</h4></label>
   </button>
 </form>
 }

关于javascript - 图像预览在 react 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690243/

相关文章:

javascript - 单击文本更改图像

javascript - 如何知道它是否真的是手机

javascript - 如何期待与 Mocha 和 Chai 的 throw ?

javascript - 在客户端压缩图像(AngularJS)并上传到服务器(NodeJS)

reactjs - 在 React 类组件中将 props 分配给变量

javascript - 如何将一个文件中的组件链接到另一个文件中的组件?

javascript - 什么是广告回传标签以及常见实现

android - 从 MediaStore.Images.Media.DATA 获取图像的方向

C++ 如何使用 16 色(4 位)位图在内存中获取屏幕截图输出

javascript - Next js 和 Apollo - Cookie 未被传递