我想知道是否可以使用现有文件的路径在NodeJS中创建文件对象(名称,大小,数据等)?我知道它可能在客户端,但我没有看到任何NodeJS。如何用NodeJS中的路径创建文件对象?
在别人的话,我想的NodeJS相同功能的工作原理:
function srcToFile(src, fileName, mimeType){
return (fetch(src)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], fileName, {type:mimeType});})
);
}
srcToFile('/images/logo.png', 'logo.png', 'image/png')
.then(function(file){
console.log(file);
});
而且输出中会像:
File {name: "logo.png", lastModified: 1491465000541, lastModifiedDate: Thu Apr 06 2017 09:50:00 GMT+0200 (Paris, Madrid (heure d’été)), webkitRelativePath: "", size: 49029, type:"image/png"…}
您正在寻找[fs.stat](https://nodejs.org/api/fs.html#fs_fs_stat_path_callback),但如果您想要文件中的数据,您需要将它与[fs .readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback)。 –