React-Admin <imageinput>将图像上传到Rails API

时间:2019-01-15 15:51:37

标签: react-admin

我正在尝试使用活动存储将图像从react-admin上传到Rails api后端。

在react-admin的文档中说:“请注意,图像上传返回一个File对象。您有责任根据您的API行为对其进行处理。例如,您可以在base64中对其进行编码,或者将其发送为多部分表单数据”。我正尝试将其作为多部分表单发送。

我一直在这里阅读,但是我找不到我想要的东西,至少是我应该如何进行的路线图。

2 个答案:

答案 0 :(得分:1)

您实际上可以在文档的dataProvider section中找到示例。

您必须装饰dataProvider才能启用数据上传。这是在发布资源之前将图像转换为base64字符串的示例:

// in addUploadFeature.js
/**
 * Convert a `File` object returned by the upload input into a base 64 string.
 * That's not the most optimized way to store images in production, but it's
 * enough to illustrate the idea of data provider decoration.
 */
const convertFileToBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file.rawFile);

    reader.onload = () => resolve(reader.result);
    reader.onerror = reject;
});

/**
 * For posts update only, convert uploaded image in base 64 and attach it to
 * the `picture` sent property, with `src` and `title` attributes.
 */
const addUploadFeature = requestHandler => (type, resource, params) => {
    if (type === 'UPDATE' && resource === 'posts') {
        // notice that following condition can be true only when `<ImageInput source="pictures" />` component has parameter `multiple={true}`
        // if parameter `multiple` is false, then data.pictures is not an array, but single object
        if (params.data.pictures && params.data.pictures.length) {
            // only freshly dropped pictures are instance of File
            const formerPictures = params.data.pictures.filter(p => !(p.rawFile instanceof File));
            const newPictures = params.data.pictures.filter(p => p.rawFile instanceof File);

            return Promise.all(newPictures.map(convertFileToBase64))
                .then(base64Pictures => base64Pictures.map((picture64, index) => ({
                    src: picture64,
                    title: `${newPictures[index].title}`,
                })))
                .then(transformedNewPictures => requestHandler(type, resource, {
                    ...params,
                    data: {
                        ...params.data,
                        pictures: [...transformedNewPictures, ...formerPictures],
                    },
                }));
        }
    }
    // for other request types and resources, fall back to the default request handler
    return requestHandler(type, resource, params);
};

export default addUploadFeature;

然后您可以将其应用于您的dataProvider:

// in dataProvider.js
import simpleRestProvider from 'ra-data-simple-rest';
import addUploadFeature from './addUploadFeature';

const dataProvider = simpleRestProvider('http://path.to.my.api/');
const uploadCapableDataProvider = addUploadFeature(dataProvider);

export default uploadCapableDataProvider;

最后,您可以照常在管理员中使用它:

// in App.js
import { Admin, Resource } from 'react-admin';

import dataProvider from './dataProvider';
import PostList from './posts/PostList';

const App = () => (
    <Admin dataProvider={uploadCapableDataProvider}>
        <Resource name="posts" list={PostList} />
    </Admin>
);

答案 1 :(得分:0)

使用文件时,请在 React 前端使用多部分表单,例如在 API 后端使用 multer。

在 react-admin 中,您应该创建一个 custom dataProvider 并扩展默认值或构建一个自定义的。每个实现你应该处理文件/文件上传。用于从 react-admin 中的自定义数据提供程序上传一个或多个文件:

// dataProvider.js
// this is only the implementation for a create

case "CREATE":
  const formData = new FormData();

  for ( const param in params.data ) {

    // 1 file
    if (param === 'file') {
      formData.append('file', params.data[param].rawFile);
      continue
    }

    // when using multiple files
    if (param === 'files') {
      params.data[param].forEach(file => {
        formData.append('files', file.rawFile);
      });
      continue
    }

    formData.append(param, params.data[param]);
  }

  return httpClient(`myendpoint.com/upload`, {
    method: "POST",
    body: formData,
  }).then(({ json }) => ({ data: json });

从那里您可以使用 multer 在您的 API 中提取它,它支持开箱即用的多部分表单。使用 nestjs 时可能如下所示:

import {
  Controller,
  Post,
  Header,
  UseInterceptors,
  UploadedFile,
} from "@nestjs/common";
import { FileInterceptor } from '@nestjs/platform-express'

@Controller("upload")
export class UploadController {

  @Post()
  @Header("Content-Type", "application/json")
  // multer extracts file from the request body 
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(
    @UploadedFile() file : Record<any, any>
  ) {
    console.log({ file })
  }
}