Reactjs 使用fetch上传图像
我定义了一个表单:Reactjs 使用fetch上传图像,reactjs,file-upload,laravel-5,redux,fetch,Reactjs,File Upload,Laravel 5,Redux,Fetch,我定义了一个表单: <form method="POST" onSubmit={this.handleSubmit.bind(this)} enctype="multipart/form-data"> fetchByArray(格式): 如果所有参数都是字符串,则此操作有效,但如果我尝试指定一个文件,则会出现错误,因为我的api处理程序无法获取该文件 在后端$request->file('photo')为空。我的问题是什么?您需要阅读文件发送api的规则。我们在与客户端文件系统相关
<form method="POST" onSubmit={this.handleSubmit.bind(this)} enctype="multipart/form-data">
fetchByArray(格式):
如果所有参数都是字符串,则此操作有效,但如果我尝试指定一个文件,则会出现错误,因为我的api处理程序无法获取该文件
在后端$request->file('photo')
为空。我的问题是什么?您需要阅读文件发送api的规则。我们在与客户端文件系统相关的操作中有很多限制
我将使用React Redux Saga描述它是如何为我工作的
您可能必须在表单数据中指定不同的选项,如下图所示
handleSubmit(event)
{
event.preventDefault();
var FormData = require('form-data');
var form = new FormData();
form.append('nome', this.refs.name.value);
form.append('phone', this.refs.phone.value);
form.append('address', this.refs.address.value);
form.append('mail', this.refs.mail.value);
form.append('type', 'file');
form.append('photo', this.refs.photo.value);
const {dispatch} = this.props;
dispatch(fetchByArray(form));
} }
export function fetchByArray(form) {
return function(dispatch) {
return fetch(`http://my_domain/api`, {
method: "POST", body: form })
.then(response => response.json())
.then(json => dispatch(receiveByQuerystring1(json)));
} }