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

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

问题描述

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

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 = "\r\n";

    //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);

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

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

上传后的相同文件显示

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

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(/\r\n/g,"\n");
        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;
    )

然后在原始代码

//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;

这让我输出

仍然不是原始的文件是=(

Still not what the raw file was =(

如何编码/加载/处理文件以避免编码问题,因此在HTTP请求中接收的文件与文件相同在上传之前。

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.

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

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.

推荐答案

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

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

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

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