【发布时间】:2015-12-27 22:00:52
【问题描述】:
为什么 base64Decode 不能解码 base64Encode 编码的东西?
function test_base64encoding_decoding() {
var file = DriveApp.getFileById("<google drive png file id>");
// Encode file bytes as a string
var base64EncodedBytes = Utilities.base64Encode(file.getBlob().getBytes(), Utilities.Charset.UTF_8);
// Decode string
var bytes = Utilities.base64Decode(base64EncodedBytes, Utilities.Charset.UTF_8);
// create new file
var blob = Utilities.newBlob(bytes, file.getMimeType(), file.getName() + ".copy.png");
file.getParents().next().createFile(blob);
}
此 google 应用程序脚本从现有的 google 驱动器源文件中检索字节并将这些字节转换为 base64 编码字符串 (base64EncodedBytes)。然后它将字符串转换回普通字节数组,并在同一文件夹中创建一个全新的文件。
现在,如果我们在 Google Drive 中查看最终结果,我们可以看到复制的文件(后缀为“.copy.png”的文件)的大小不同并且已损坏。
这种编码/解码 API 使用有什么问题?
【问题讨论】:
标签: google-apps-script base64 decode utilities