Search code examples
reactjsadmin-on-restreact-dropzone

Is it possible to upload multiple files, get their ids and then bind those ids to the parent resource with ImageInput?


from what I have seen all the action happens when I press submit of the form that contains:

<ImageInput multiple source="pictures" accept="image/*">
  <ImageField source="src" title="title" />
</ImageInput>

then some code like that is being run:

const addUploadCapabilities = requestHandler => (type, resource, params) => {
    if ( (type === 'CREATE' || type === "UPDATE")) {
        if (params.data.pictures && params.data.pictures.length) {
            const pictures = params.data.pictures.filter(p => p.rawFile instanceof File)

            const url = config.uploadImageEndpoint;
            //the params contain the image as a fileInstance
            params.data.pictures = [];
            for (let picture of pictures) {
                let form = new FormData();
                form.append('file', picture.rawFile);
                let options = APIUtils.createOptionsForImageUpload(form);
                return fetchUtils.fetchJson(url, options)
                .then(res => {
                    params.data.pictures.push({id: res.json.content[0].id});
                    return requestHandler(type, resource, params)
                });
            }
        }
    }

    return requestHandler(type, resource, params);
};

and then the restClient code is run to save the resource of the form on either create or edit..

I am curious how to get the ids after those files are saved in the database.. and then submit the form and connect those ids to the specific ImageInputs..

Edit:

With the above code I get only an array with the first id of my pictures

Thanks!


Solution

  • No clear canonical mechanism for that exists right now. AOR expects us to handle image upload at the API end with 1 call only. Above would have been a useful feature to have I must say.

    A possible way to achieve this would be to let the addUploadCapabilities method above also make a fetch call and call requestHandler asynchronously. after response from the server. Then we can modify the request parameters and continue as previously.

    const addUploadCapabilities = requestHandler => (type, resource, params) => {
        if ( (type === 'CREATE' || type === "UPDATE")) {
            if (params.data.pictures && params.data.pictures.length) {
                debugger;
    
    const pictures = params.data.pictures.filter(p => p.rawFile instanceof File)
              let form = new FormData()
              const url = imageUploadURL
              const options = {}
              //the params contain the image as a fileInstance
              form.append('image', params.data.pictures[0].rawFile);         
              options.method = 'POST'
              options.body = form
              return fetchUtils.fetchJson(url, options)
              .then(res => {
                   params.data.pictureId = res.json.id
                   return requestHandler(type, resource, params)
                  })
    
                );
            }
        }
    
        return requestHandler(type, resource, params);
    };