前端代码App.js:
import React from "react";
import axios from "axios";
export default class App extends React.Component {
constructor() {
super();
this.state = {
img1 : ''
}
}
render() {
return (
<div>
<input type="file" name="file" multiple="multiple" id="uploadimg1" onChange={() => this.getimg1()}/>
<br />
<button onClick={() => this.getcomparisonresult()}>提交</button>
</div>
)
}
getimg1() {
let fileData = document.querySelector("#uploadimg1").files[0];
let formdata = new FormData();
formdata.append("img1", fileData);
this.setState({
img1: formdata
})
}
getcomparisonresult() {
let headers = {
'Content-Type': 'multipart/form-data'
}
axios.post(
"http://127.0.0.1:5000/student/", this.state.img1,{headers: headers}
).then(res => {
console.log(res.data)
}).catch(err => {
console.log("错误")
console.error(err);
});
}
}
后端python端api.py:
from flask import Flask,jsonify,request
from flask_cors import *
import cv2
import numpy as np
app = Flask(__name__)
CORS(app,supports_credentials = True)
#访问接口:http://127.0.0.1:5000/student
#请求方式:POST
@app.route('/student/',methods=['POST'])
def student():
student_name = request.files.to_dict()
image1 = cv2.imdecode(np.asarray(bytearray(student_name['img1'].read()),dtype='uint8'), cv2.IMREAD_COLOR)
cv2.imshow('img1', image1)
if __name__ == '__main__':
app.run(debug=True)