如何使用 React 上传文件? [英] How to upload files using React?
问题描述
所以目前我正在尝试上传文件,并将其保存在 Google Clouds 上.但是我被困在如何使用 React-Redux 获取输入文件.
so currently I am trying to upload a file, and save it on the Google Clouds. But I am stuck at how to get the input file using React-Redux.
基本上,我的后端部分已经完成,我使用 HTTPie 和此命令对其进行了测试HTTP localhost:8000/v1/... @(file_path)
它工作得很好.
Basically, my back-end part already finish, I tested it using HTTPie with this command
HTTP localhost:8000/v1/... @(file_path)
and it works perfectly fine.
现在在我使用输入标签之后:我不知道如何获取用户选择的 file_path.而且我也不知道用这种方法能不能拿到上传的文件.
Now after I use input tag:
<input type="file" onChange="">
I do not know how to get the file_path that are the user choose.
And I dont even know whether I can get the uploaded file using this method.
谢谢
推荐答案
你不能通过方法<input type="file" onChange={this.handleChangeFile.bind(this)}>代码>
handleChangeFile(event) {
const file = event.target.files[0];
let formData = new FormData();
formData.append('file', file);
//Make a request to server and send formData
}
这篇关于如何使用 React 上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!