Python 将文件从react上载到flask时出错
我尝试使用React上传文件,并使用post请求将其传递到flask后端。但由于Python 将文件从react上载到flask时出错,python,reactjs,flask,Python,Reactjs,Flask,我尝试使用React上传文件,并使用post请求将其传递到flask后端。但由于加载资源失败,我收到了错误消息:服务器响应状态为400(错误请求)。 我想不出这个问题。如果有任何其他可能的方法,这也将是有益的 这是upload.js: import axios from 'axios'; import React from 'react'; class UploadVideo extends React.Component { constructor(props) { super(
加载资源失败,我收到了错误消息:服务器响应状态为400(错误请求)
。
我想不出这个问题。如果有任何其他可能的方法,这也将是有益的
这是upload.js:
import axios from 'axios';
import React from 'react';
class UploadVideo 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);
console.log(data)
axios('/upload', {
method: 'POST',
body: data,
}).then((response) => {
console.log(response)
}).catch(err => {
console.log(err)
});
}
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 type="submit" className="btn">Upload</button>
</div>
<img src={this.state.imageURL} alt="img" />
</form>
);
}
因为react和flask在不同的端口中运行,所以使用完整的URL。检查下面的代码
handleUploadImage(ev) {
ev.preventDefault();
const data = new FormData();
data.append('file', this.uploadInput.files[0]);
data.append('filename', this.fileName.value);
console.log(data)
axios('http://localhost:8000/upload', {
method: 'POST',
body: data,
}).then((response) => {
console.log(response)
}).catch(err => {
console.log(err)
});
}
您可以为react和flask应用共享您的本地主机url吗。它的thisso react在本地主机上运行:3000?不,react在本地主机上运行:3500它说url必须以前导的斜杠开头。对不起,我以为我编辑了react代码,请检查编辑是否错误为400(错误请求)在浏览器中打开网络控制台,检查您从flask应用程序收到的上传请求的确切错误消息。还要检查文件对象是否正在请求正文中发送
handleUploadImage(ev) {
ev.preventDefault();
const data = new FormData();
data.append('file', this.uploadInput.files[0]);
data.append('filename', this.fileName.value);
console.log(data)
axios('http://localhost:8000/upload', {
method: 'POST',
body: data,
}).then((response) => {
console.log(response)
}).catch(err => {
console.log(err)
});
}