首页 > 解决方案 > 在 laravel 中上传具有多个文件的元数据

问题描述

我想用元数据在 laravel 中上传多个文件。像文件自定义名称和文件类别。

我尝试使用 js 在前端添加一些属性,但我可以在控制器中看到它们这里的代码


$(document).on('change','#file',function(e){
  let file = e.target.files[0]

  file.totalWords = 1234;
  file.caytegory = 'category';
          
  filesList.push(file)
});

let mainFormData = new FormData();
    filesList.forEach(file => { 
      mainFormData.append('files[]',file)
    })


$.ajax({
      data: mainFormData,
      url: 'controller_url',
      type: "POST",
      processData: false,
      contentType: false,
      headers: {
        Accept : "application/json"
    },
    success: function (data) {
       console.log(data)
      });
    },
    error: function(response) {
        console.log(response);
    }
});


这是我的控制器

return response()->json($request->file('files')[0]->caytegory);

我想要的只是从提交的文件中访问类别属性,但我不能

标签: javascriptphpajaxlaravelfile

解决方案


推荐阅读