如何使用 fs 将文件存储到解析服务器

How to Store a File to Parse Server with fs

我遇到了这个问题,虽然这可能是一个菜鸟问题。我想将 ../images/icon.png 处的一些文件作为 File 对象存储在我的数据库中。我无法访问实际数据并正确存储它。 Parse.File 的文档说您可以访问数据作为

1. an Array of byte value Numbers, or 
2. an Object like { base64: "..." } with a base64-encoded String. 
3. a File object selected with a file upload control.

但我不知道该怎么做。

我最后做的是

let iconFile = undefined;
  const filePath = path.resolve(__dirname, '..', 'images/icon.png');

  fs.readFile(filePath, 'base64', function(err, data) {
    if (err) {
      console.log(err);
    } else {
      iconFile = new Parse.File('icon', {base64: data});
    }
  });

我在路径未正确指向图像的地方遇到错误,所以我使用了节点的路径(如 require('path') 中那样使其正确指向。

据我所知,此代码适用于任何文件类型。