是否可以上传多个文件,获取它们的 ID,然后使用 ImageInput 将这些 ID 绑定到父资源?

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

据我所知,当我按下包含以下内容的表单的提交时,所有操作都会发生:

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

然后就是这样的一些代码 运行:

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

然后 restClient 代码是 运行 在创建或编辑时保存表单资源..

我很好奇这些文件保存在数据库中后如何获取 ID.. 然后提交表单并将这些 ID 连接到特定的 ImageInputs..

编辑:

通过上面的代码,我只得到一个数组,其中包含我的图片的第一个 id

谢谢!

目前尚无明确的规范机制。 AOR 希望我们仅通过 1 次调用即可在 API 端处理图像上传。我必须说,以上是一个有用的功能。

实现此目的的一种可能方法是让上面的 addUploadCapabilities 方法也进行提取调用并异步调用 requestHandler。服务器响应后。然后我们可以修改请求参数并像以前一样继续。

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

另一种方法是使用自定义输入处理上传。参见 admin on rest send extra params to rest call

根据 kunal pareek 的回答,这是收集所有 ID 的完整代码,然后您可以将它们绑定到父资源:

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 = "some url";
            //the params contain the image as a fileInstance
            params.data.pictures = [];
            let promises = [];
            for (let picture of pictures) {
                let form = new FormData();
                form.append('file', picture.rawFile);
                let options = // create options without content type and body as form
                let promise = fetchUtils.fetchJson(url, options);
                promises.push(promise);
            }
            return Promise.all(promises).then(results => {
                for (let res of results) {
                    params.data.pictures.push(res.json.content[0].id);
                }
                return requestHandler(type, resource, params);
            });
        }
    }

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

这是我用于多个拖放区输入图片 1、图片 2 的版本,它在更新期间不会删除现有图像并再次传递这些 ID 以绑定到资源..

const detectPicturesAndUpload = function(properties, params, requestHandler, type, resource) {
    let promises = [];
    for (let prop of properties) {
        if (params.data[prop] && params.data[prop].length) {
            const pictures = params.data[prop].filter(p => p.rawFile instanceof File);
            const alreadUploadedPics = params.data[prop].filter(p => !p.rawFile);
            const url = config.uploadImageEndpoint;
            //the params contain the image as a fileInstance
            params.data[prop] = [];

            for (let picture of pictures) {
                let form = new FormData();
                form.append('file', picture.rawFile);
                form.append('property', prop); // the endpoint needs to return the prop name
                let options = APIUtils.createOptionsForImageUpload(form);
                let promise = fetchUtils.fetchJson(url, options);
                promises.push(promise);
            }
            for (let uploadedPic of alreadUploadedPics) {
                if (!Array.isArray(params.data[prop])) {
                    params.data[prop] = [];
                }
                params.data[prop].push(parseInt(uploadedPic.id));
            }
        }
    }
    return Promise.all(promises).then(results => {
        for (let res of results) {
            let propName = res.json.content[0].property; // so it can be used here
            if (!Array.isArray(params.data[propName])) {
                params.data[propName] = [];
            }
            params.data[propName].push(parseInt(res.json.content[0].id));
        }
        return requestHandler(type, resource, params);
    });
}


const addUploadCapabilities = requestHandler => (type, resource, params) => {
    if ( (type === 'CREATE' || type === "UPDATE")) {
        return detectPicturesAndUpload(["pictures1", "pictures2"], params, requestHandler, type, resource);
    }

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

export default addUploadCapabilities;