简体   繁体   English

FileReader - 如何将字符编码从 UTF-8 更改为 ANSI

[英]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。

Modification points :改装要点:

  • Use readAsDataURL() instead of readAsText() .使用readAsDataURL()而不是readAsText()
  • Define data as base64 in request body.在请求正文中将数据定义为 base64。

Modified script :修改后的脚本:

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

Reference :参考 :

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.

相关问题 将 Javascript BLOB 编码更改为 ANSI 而不是 UTF-8 - Change Javascript BLOB encoding to ANSI instead of UTF-8 JavaScript blob编码为UTF-8而不是ANSI - JavaScript blob encoding as UTF-8 instead of ANSI 将 FileReader 的结果编码改为 UTF8 - Change the encoding of FileReader's result to UTF8 Javascript FileReader readAsText函数不会低于ä和ö这样的utf-8编码字符 - Javascript FileReader readAsText function not understaning utf-8 encoding characters like ä and ö 使用记事本将 JavaScript 文件保存为 Ansi 或 UTF-8 编码 - Save JavaScript files using Notepad as Encoding of Ansi or UTF-8 在Javascript中将编码从UTF-8更改为ISO-8859-2 - Change encoding from UTF-8 to ISO-8859-2 in Javascript 从Firefox复制后,Url中的UTF-8字符发生变化 - UTF-8 character in Url change when after copy it from firefox 如何使用nodejs-iconv模块(或其他解决方案)在nodejs javascript中将字符编码从CP932转换为UTF-8 - How to convert character encoding from CP932 to UTF-8 in nodejs javascript, using the nodejs-iconv module (or other solution) 编码和JS-如何从UTF-8文本中删除带有JavaScript的重音符号 - Encoding and JS - how to remove accents with javascript from a UTF-8 text 在Java中将ANSI转换为UTF-8 - converting ANSI to UTF-8 in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM