Node: converting utf8 string to base64 to upload a

2019-03-06 03:56发布

I'm not sure if this question is specific to Parse.com or a problem with my (basic) understanding of NodeJS encoding.

I've been using Parse to upload text files - this is working great but it looks like the file contents are not UTF8 encoded (based on what I see from the data explorer in my browser).

Is there something I should be doing before saving the file to ensure the text is stored as unicode?

The code I'm using to upload is:

//test string
var a = '検索 • Busca • Sök • 搜尋 • Tìm kiếm • Пошук';

//trying to convert to base64
var buff = new Buffer(a, 'utf8').toString("base64");

var parseFile = new Parse.File("test.txt", {
    base64: buff
}, 'text/plain');

//saves successfully
parseFile.save().then(function() {
    var Test = Parse.Object.extend('Testing');
    var uv = new Test();
    uv.set('hello', parseFile);
    return uv.save(null, {});


}).then(function() {
    console.log('Saved');
}, function(error) {
    console.error(JSON.stringify(error));
});

When I look at test.txt from the data explorer, I see:

検索 • Busca • Sök • æœå°‹ • Tìm kiếm • Пошук

When I try to use the same code above to save to disk using require('fs'), everything works as expected.

1条回答
孤傲高冷的网名
2楼-- · 2019-03-06 04:22

The data explorer you're using thinks the file is UTF-16 bytes encoded into Base-64 (this can easily be verified). You shouldn't worry, the data is correct, the representation is wrong. The data viewer isn't going to change your data, so it doesn't matter.

查看更多
登录 后发表回答