具有自定义属性的文件对象

Ber*_*and 6 file form-data node.js reactjs multer

我想使用自定义文件属性将文件存储在服务器上。在客户端即时添加属性:

let file = new File([blob], 'flower.jpg')
file.custom = "another properties"
Run Code Online (Sandbox Code Playgroud)

这给了我

custom:"another properties"
lastModified:1524742832101
lastModifiedDate:Thu Apr 26 2018 13:40:32 GMT+0200 (W. Europe Daylight Time {}
name:"flower.jpg"
size:845941
type:"image/jpeg"
webkitRelativePath:""
Run Code Online (Sandbox Code Playgroud)

当我将此文件发送到我的节点服务器时,自定义属性被删除。我使用 formData 和 multer 进行文件上传。

fieldname: 'images',
originalname: 'flower.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'uploads/',
size: 845941
Run Code Online (Sandbox Code Playgroud)

有没有办法存储包含自定义属性的文件?

小智 3

我在 multer /express 中遇到了类似的情况,最终为每个上传的文件附加了一个附加属性。然后从服务器上的 req.body 中提取与文件名匹配的附加属性。我们的 UI 可以防止重复的文件名,因此这对我们来说效果很好。

const data = new FormData();

files.forEach((file) => {
  data.append('form-key', file);
  data.append(file.name, file.identifier);
});
Run Code Online (Sandbox Code Playgroud)