[英]FileReader - How to change the character encoding from UTF-8 to ANSI
I am trying to upload files to Google Drive through the API provided for the product.我正在尝试通过为产品提供的 API将文件上传到Google Drive 。 However, I think there is a problem with the encoding - in particular, the FileReader.readAsText()
function.但是,我认为编码存在问题 - 特别是FileReader.readAsText()
函数。
I tried adding the optional parameter for encoding to make it look like r.readAsText(f,'ANSI')
but it seems to not work.我尝试为编码添加可选参数以使其看起来像r.readAsText(f,'ANSI')
但它似乎不起作用。
The readAsText()
function defaults to UTF-8 encoding for some weird reason .由于某些奇怪的原因, readAsText()
函数默认为 UTF-8 编码。 When I try to upload image files with UTF-8 encoding, they're corrupted and do not open properly.当我尝试使用 UTF-8 编码上传图像文件时,它们已损坏且无法正常打开。
function readFile(evt) {
var fData = [];
var f = evt.target.files[0];
if (f) {
var r = new FileReader();
r.readAsText(f);
fData.unshift(f.name, f.type);
var myInt = setInterval(function() {
if (r.readyState == 2) {
fData.push(r.result);
uploadFiles(fData);
clearInterval(myInt);
}
}, 50);
} else {
alert("Failed to load file");
}
}
function uploadFiles(dataArray,callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
const contentType = 'application/json';
var metadata = {
'name': dataArray[0],
'mimeType': dataArray[1]
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n\r\n' +
dataArray[2] +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': { 'uploadType': 'multipart' },
'headers': {
'Content-Type': 'multipart/related; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
document.getElementById('upFile').addEventListener('change', readFile, false);
You want to upload files to Google Drive using Drive API by JavaScript.您想通过 JavaScript 使用 Drive API 将文件上传到 Google Drive。 How about this workaround?这个解决方法怎么样? In this workaround, the file is converted to base64 and uploaded to Google Drive.在此解决方法中,文件将转换为 base64 并上传到 Google Drive。
readAsDataURL()
instead of readAsText()
.使用readAsDataURL()
而不是readAsText()
。Please modify 2 parts as follows.请修改2部分如下。
1. From : 1. 来自:var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n\r\n' +
dataArray[2] +
close_delim
1. To :
1. 致:
r.readAsDataURL(f);
2. From :
2. 来自:
var multipartRequestBody = delimiter + 'Content-Type: application/json\\r\\n\\r\\n' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + contentType + '\\r\\n\\r\\n' + dataArray[2] + close_delim
2. To :
2. 致:
var multipartRequestBody = delimiter + 'Content-Type: application/json\\r\\n\\r\\n' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + contentType + '\\r\\n' + // Modified 'Content-Transfer-Encoding: base64\\r\\n\\r\\n' + // Added dataArray[2].split(",")[1] + // // Modified close_delim
Also I have experienced the same situation with you.我也遇到过和你一样的情况。 At that time, I resolved this using base64.当时,我使用base64解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.