本文介绍了节点:将 utf8 字符串转换为 base64 以作为 Parse.com 文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这个问题是特定于 Parse.com 还是我对 NodeJS 编码的(基本)理解有问题.

我一直在使用 Parse 上传文本文件 - 这很好用,但看起来文件内容不是 UTF8 编码的(基于我从浏览器中的数据浏览器看到的内容).

在保存文件之前我应该​​做些什么以确保文本存储为 unicode?

我用来上传的代码是:

//测试字符串var a = '検索 • Busca • Sök • 搜索 • Tìm kiếm • Пошук';//尝试转换为base64var buff = new Buffer(a, 'utf8').toString("base64");var parseFile = new Parse.File("test.txt", {base64:增益}, '文本/普通');//保存成功parseFile.save().then(function() {var Test = Parse.Object.extend('Testing');var uv = 新测试();uv.set('你好', parseFile);返回 uv.save(null, {});}).那么(函数(){console.log('已保存');}, 函数(错误){控制台错误(JSON.stringify(错误));});

当我从数据浏览器查看 test.txt 时,我看到:

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

当我尝试使用上述相同的代码使用 require('fs') 保存到磁盘时,一切都按预期工作.

解决方案

您使用的数据浏览器认为该文件是编码为 Base-64 的 UTF-16 字节(这很容易验证).你不用担心,数据是正确的,表示是错误的.数据查看器不会更改您的数据,因此没有关系.

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.

解决方案

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.

这篇关于节点:将 utf8 字符串转换为 base64 以作为 Parse.com 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:35