fileReader.readAsBinaryString 上传文件 [英] fileReader.readAsBinaryString to upload files

查看:54
本文介绍了fileReader.readAsBinaryString 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 fileReader.readAsBinaryString 通过 AJAX 将 PNG 文件上传到服务器,精简代码(fileObject 是包含我的文件信息的对象);

var fileReader = new FileReader();fileReader.onload = 函数(e){var xmlHttpRequest = new XMLHttpRequest();//一些 AJAX-y 的东西 - 回调、处理程序等.xmlHttpRequest.open("POST", '/pushfile', true);var dashes = '--';var边界 = 'aperturephotoupload';var crlf = "
";//使用正确的 MIME 类型发布(如果操作系统可以识别)if ( fileObject.type == '' ){filetype = '应用程序/八位字节流';} 别的 {文件类型 = fileObject.type;}//构建一个HTTP请求来发布文件var 数据 = 破折号 + 边界 + crlf + "Content-Disposition: form-data;"+ "名称="文件";"+ "filename="" + unescape(encodeURIComponent(fileObject.name)) + """ + crlf + "Content-Type:" + filetype + crlf + crlf + e.target.result + crlf + 破折号 + 边界 +破折号;xmlHttpRequest.setRequestHeader("Content-Type", "multipart/form-data;boundary="+boundary);//发送二进制数据xmlHttpRequest.send(data);}fileReader.readAsBinaryString(fileObject);

在上传前检查文件的前几行(使用 VI)给我

上传后显示同一个文件

所以它看起来像某个地方的格式/编码问题,我尝试在原始二进制数据上使用简单的 UTF8 编码函数

 函数 utf8encode(string) {string = string.replace(/
/g,"
");var utftext = "";for (var n = 0; n < string.length; n++) {var c = string.charCodeAt(n);如果 (c <128) {utftext += String.fromCharCode(c);}否则 if((c > 127) && (c < 2048)) {utftext += String.fromCharCode((c >> 6) | 192);utftext += String.fromCharCode((c & 63) | 128);}别的 {utftext += String.fromCharCode((c >> 12) | 224);utftext += String.fromCharCode(((c >> 6) & 63) | 128);utftext += String.fromCharCode((c & 63) | 128);}}返回utftext;)

然后在原代码中

//构建一个HTTP请求来发布文件var 数据 = 破折号 + 边界 + crlf + "Content-Disposition: form-data;"+ "名称="文件";"+ "filename="" + unescape(encodeURIComponent(file.file.name)) + """ + crlf + "Content-Type:" + filetype + crlf + crlf + utf8encode(e.target.result) + crlf+ 破折号 + 边界 + 破折号;

这给了我

的输出

仍然不是原始文件是什么 =(

如何对文件进行编码/加载/处理以避免编码问题,使 HTTP 请求中接收的文件与上传前的文件相同.

一些其他可能有用的信息,如果我不使用 fileReader.readAsBinaryString() 而是使用 fileObject.getAsBinary() 来获取二进制数据,则它工作正常.但是 getAsBinary 仅适用于 Firefox.我一直在 Mac 上的 Firefox 和 Chrome 中对此进行测试,两者都得到了相同的结果.后端上传由 NGINX 上传模块处理,再次运行苹果电脑.服务器和客户端在同一台机器上.我尝试上传的任何文件都会发生同样的事情,我只是选择了 PNG,因为它是最明显的例子.

解决方案

使用 fileReader.readAsDataURL( fileObject ),这会将其编码为 base64,您可以安全地上传到您的服务器.>

Trying to use fileReader.readAsBinaryString to upload a PNG file to the server via AJAX, stripped down code (fileObject is the object containing info on my file);

var fileReader = new FileReader();

fileReader.onload = function(e) {
    var xmlHttpRequest = new XMLHttpRequest();
    //Some AJAX-y stuff - callbacks, handlers etc.
    xmlHttpRequest.open("POST", '/pushfile', true);
    var dashes = '--';
    var boundary = 'aperturephotoupload';
    var crlf = "
";

    //Post with the correct MIME type (If the OS can identify one)
    if ( fileObject.type == '' ){
        filetype = 'application/octet-stream';
    } else {
        filetype = fileObject.type;
    }

    //Build a HTTP request to post the file
    var data = dashes + boundary + crlf + "Content-Disposition: form-data;" + "name="file";" + "filename="" + unescape(encodeURIComponent(fileObject.name)) + """ + crlf + "Content-Type: " + filetype + crlf + crlf + e.target.result + crlf + dashes + boundary + dashes;

    xmlHttpRequest.setRequestHeader("Content-Type", "multipart/form-data;boundary=" + boundary);

    //Send the binary data
    xmlHttpRequest.send(data);
}

fileReader.readAsBinaryString(fileObject);

Examining the first few lines of a file before upload (using VI) gives me

The same file after upload shows

So it looks like a formatting/encoding issue somewhere, I tried using a simple UTF8 encode function on the raw binary data

    function utf8encode(string) {
        string = string.replace(/
/g,"
");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    )

Then in the original code

//Build a HTTP request to post the file
var data = dashes + boundary + crlf + "Content-Disposition: form-data;" + "name="file";" + "filename="" + unescape(encodeURIComponent(file.file.name)) + """ + crlf + "Content-Type: " + filetype + crlf + crlf + utf8encode(e.target.result) + crlf + dashes + boundary + dashes;

which gives me the output of

Still not what the raw file was =(

How do I encode/load/process the file to avoid the encoding issues, so the file being received in the HTTP request is the same as the file before it was uploaded.

Some other possibly useful information, if instead of using fileReader.readAsBinaryString() I use fileObject.getAsBinary() to get the binary data, it works fine. But getAsBinary only works in Firefox. I've been testing this in Firefox and Chrome, both on Mac, getting the same result in both. The backend uploads are being handled by the NGINX Upload Module, again running on Mac. The server and client are on the same machine. The same thing is happening with any file I try to upload, I just chose PNG because it was the most obvious example.

解决方案

Use fileReader.readAsDataURL( fileObject ), this will encode it to base64, which you can safely upload to your server.

这篇关于fileReader.readAsBinaryString 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆