使用ReactJS渲染图像从flask-restful API接收

时间:2018-05-12 13:24:31

标签: javascript reactjs flask flask-restful

我发送了一个带有烧瓶的图像,但我无法使用ReactJS进行渲染。我尝试使用btoa和blob来解决问题,但没有结果。这是我写的代码

烧瓶安息代码:

class UploadImage(Resource):
    UPLOAD_FOLDER = 'img'
    parser = reqparse.RequestParser()
    parser.add_argument('file', 
          type=werkzeug.datastructures.FileStorage,
    location='files',
    required=True, help='Must provide username')

def post(self):
    data = self.parser.parse_args()
    if data['file'] == "":
        return {
            'data':'',
            'message': 'No File Found',
            'status': 'Error'
        }
    photo = data['file']

    if photo:
        filename = 'input.png'
        path = os.path.join(self.UPLOAD_FOLDER,filename)
        photo.save(path)
    # with open(path, "rb") as f:
    #     encoded_image = f.read()
    #     print(type(encoded_image))
        return send_file(path, mimetype='image/png')

React代码:

onButtonClick = () => {
    const fd = new FormData();
    fd.append('file', this.state.image, this.state.image.name)

    axios.post('http://localhost:5000/image', fd).then(res => {
      this.setState({
        features: res.data
      })

    })
  }
render() {
    const { features } = this.state
    return (
      <div className="App">
        <input
        type='file'
        onChange={this.onInputChange}
        />
        <button onClick={this.onButtonClick}>Upload</button>
        <br />
        {features && <img src={features} alt='some text'/>}
      </div>
    );
  }

有人可以帮我解决这个问题谢谢。

0 个答案:

没有答案