我已经阅读了很多有关fetch和ReactJS的文档,但是我根本找不到我的代码的问题,情况是我可以选择并存储所需状态的文件,但是当我发送提交时,Flask无法接收该文件,仅此消息:
127.0.0.1--[08 / May / 2020 18:50:44]“ POST / upload HTTP / 1.1” 200-
这是我的ReactJS代码:
import React from 'react';
class Main extends React.Component {
constructor(props) {
super(props);
this.state = {
file: null,
brand: null
};
this.handleFile = this.handleFile.bind(this);
this.handleBrand = this.handleBrand.bind(this);
this.handleUpload = this.handleUpload.bind(this);
}
handleBrand(e) {
let brand = e.target.value
this.setState({brand: brand});
}
handleFile(e){
let file = e.target.files
this.setState({file: file})
console.log(file)
}
handleUpload(e){
e.preventDefault();
const data = new FormData();
data.append('file', this.state.file);
console.log(this.state.file)
fetch('http://localhost:5000/upload', {
method: 'POST',
body: data,
})
}
render() {
return (
<form onSubmit={this.handleUpload}>
<div>
<input onChange={this.handleFile} id="fileToUpload" type="file" name="filesToUpload" id="filesToUpload"/>
</div>
<div>
<select value={this.state.value} onChange={this.handleBrand}>
<option value="">- Please select -</option>
<option value="toyota">Toyota</option>
<option value="quick_lanes">Quick Lanes</option>
<option value="volvo">Volvo</option>
<option value="alfa_romeo">Alfa Romeo</option>
</select>
</div>
<br />
<div>
<button>Upload</button>
</div>
</form>
);
}
}
export default Main;
这是我的Flask代码:
from flask import *
from flask_cors import *
import os
app = Flask(__name__)
CORS(app)
UPLOAD_FOLDER = "C:/projects/poi_dealers/files"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@app.route("/upload", methods=["GET", "POST"])
def upload_image():
if request.method == "POST":
if request.files:
filesToUpload = request.files["file"]
filesToUpload.save(os.path.join(app.config["UPLOAD_FOLDER"], filesToUpload.filename))
print(filesToUpload)
return redirect(request.url)
return "All ok"
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.run(debug=True, host="0.0.0.0", use_reloader=False)
事情就是这样,我在网上找到了此代码,它可以正常工作!我不明白为什么,我已经从上至下阅读了,看不到差异,我不想使用此代码,我想了解正在发生的事情,有什么建议吗?我在这里找到的代码:
import React from 'react';
class Main extends React.Component {
constructor(props) {
super(props);
this.state = {
imageURL: '',
};
this.handleUploadImage = this.handleUploadImage.bind(this);
}
handleUploadImage(ev) {
ev.preventDefault();
const data = new FormData();
data.append('file', this.uploadInput.files[0]);
data.append('filename', this.fileName.value);
fetch('http://localhost:5000/upload', {
method: 'POST',
body: data,
}).then((response) => {
response.json().then((body) => {
this.setState({ imageURL: `http://localhost:5000/${body.file}` });
});
});
}
render() {
return (
<form onSubmit={this.handleUploadImage}>
<div>
<input ref={(ref) => { this.uploadInput = ref; }} type="file" />
</div>
<div>
<input ref={(ref) => { this.fileName = ref; }} type="text" placeholder="Enter the desired name of file" />
</div>
<br />
<div>
<button>Upload</button>
</div>
<img src={this.state.imageURL} alt="img" />
</form>
);
}
}
export default Main;
这是python终端中的输出(当然,它可以正确保存文件,“ competitive_process.py”是im用于测试目的的文件名):
<FileStorage: 'competitive_process.py' ('text/plain')>
127.0.0.1 - - [08/May/2020 18:39:09] "POST /upload HTTP/1.1" 302 -
127.0.0.1 - - [08/May/2020 18:39:09] "GET /upload HTTP/1.1" 200 -
PS:幸运的是,没有CORS错误,我三查了
问候 !
您可以尝试一下,看看是否有帮助?