2017-07-17 41 views
0

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

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. 

但我不知道如何实际做到这一点。

回答

0

我最终什么事做了

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')得到它正确地指向。

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

相关问题