jeudi 24 novembre 2016

Upload a image with fetch

Good night,

I'm a problem. I have a form

<form method="POST" onSubmit={this.handleSubmit.bind(this)} enctype="multipart/form-data">

In function submit:

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

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

This is ok, if all parameters is text but when I put a file I have a error, my api don't catch the file,

$request->file('photo')

is empty. What the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire