使用 FormData 从 React 应用程序上传 Sailsjs 文件不起作用

问题描述

我的 React 文件上传代码

   let formData = new FormData()
            console.log(values);
            
            await formData.append('profile_picture[]',values.profile_picture.rawFile,values.profile_picture)
            await formData.append('email_address',values.email_address)
            await formData.append('full_name',values.full_name)
            await formData.append('password',values.password)
            await formData.append('confirm_password',values.confirm_password)
            // console.log(formData);
            
            await fetch('http://localhost:1337/api/moderators',{
                body:formData,method:'POST',credentials:'include'
            })

我的 Sailjs 控制器

const { StatusCodes } = require("http-status-codes");

module.exports = {


  friendlyName: 'Create moderator',description: 'Functon used to create a Moderator',files: ["profile_picture"],inputs: {
    email_address: {
      type: "string",description: "The email address foser.",required: true,unique: true,isEmail: true,maxLength: 200,example: "carol.reyna@microsoft.com",},password: {
      type: "string",description: "Password of the User",protect: true,confirm_password: {
      type: "string",full_name: {
      type: "string",description: "Full representation of the user's name",maxLength: 120,example: "Lisa Microwave van der Jenny",profile_picture: {
      type: "ref",required: false,description: "File Data",exits: {
    invalid: {
      responseType: "badRequest",description:
        "The provided fullName,password and/or email address are invalid.",extendedDescription:
        "If this request was sent from a graphical user interface,the request " +
        "parameters should have been validated/coerced _before_ they were sent.",emailAlreadyInUse: {
      statusCode: StatusCodes.CONFLICT,description: "The provided email address is already in use.",noFileAttached: {
      description: "No file was attached.",responseType: "badRequest",tooBig: {
      description: "The file is too big.",fn: async function (inputs) {
    // let { profile_picture } = inputs;

    console.log(this.req.file('profile_picture'))
    await this.req.file('profile_picture').upload(
      {
        dirname: require("path").resolve(
          sails.config.appPath,"assets/images/uploads/test"
        ),maxBytes: 3000000,async (err,result) => {
        console.log(err,result)
      }
    );
    return;

  }


};

如果我从控制器中删除输入,它可以正常工作

this.req.file('profile_picture').upload()..

但我无法通过使用输入 {}

使用sailsjs 提供的自动验证

我希望能够使用输入,同时希望使用

上传图像
inputs.profile_picture.upload()...

当我使用 ejs 表单时,代码工作得很好,但在 React 中却没有,它迫使我删除输入验证。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)