Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用fetch上传图像_Reactjs_File Upload_Laravel 5_Redux_Fetch - Fatal编程技术网

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)));
        } }