dongshadu4498 2011-04-25 21:34
浏览 301
已采纳

实际上是否可以使用.send()发送二进制XHR请求?

is it actually possible to send binary XHR request using .send()? NOT using .sendAsBinary() (which is poorly supported anyway). My approach so far looks like this:

var data    = base64_decode(image);

// photo file
var part = 'Content-Disposition: form-data; name="file1"; filename="me.jpg"' + CRLF + "Content-Type: image/jpeg" + CRLF + CRLF + data + CRLF;

//console.log( base64_encode(element.files[0].getAsBinary()) );

parts.push(part);

// prepare the query
var request = 'Content-Type: multipart/form-data; boundary=' + boundary + CRLF + CRLF; 
    // content-length is missing    
    request += "--" + boundary + CRLF;
    request += parts.join("--" + boundary + CRLF);
    request += "--" + boundary + "--" + CRLF;

// send the data
var xhr      = new XMLHttpRequest();

//xhr.open('post', 'photos_upload.php', true);
xhr.open('post', '/trash/upload.php', true);

xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
xhr.setRequestHeader('Content-Length', String(request.length));

xhr.onreadystatechange = function()
{
    if(xhr.readyState === 4 && xhr.responseCode === 200)
    {
        var response            = xhr.responseText;
        var temp                = response.match(/photo.php\?fbid=(\d+)/)[1];
        var temp2               = response.match(new RegExp(temp + '_(\\d+)_(\\d+)'));
        var photo_id            = temp2[1];
        var photo_pid           = temp2[2];

        var friends_for_tags    = array_chunk(friends, number_of_tags)[0];

        for(i in friends_for_tags)
        {
            if(friends_for_tags.hasOwnProperty(i))
            {
                tag_photo(photo_id, photo_pid, friends_for_tags[i].text, friends_for_tags[i].uid);
            }
        }
    }

};

console.log(request);

xhr.send(request);

image is base64 encoded image file. However, this is what $_FILES return on server side:

array(1) {
  ["file1"]=>
  array(5) {
    ["name"]=>
    string(6) "me.jpg"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(0) ""
    ["error"]=>
    int(3)
    ["size"]=>
    int(0)
  }
}

This is how the XHR request looks in the console.log:

Content-Type: multipart/form-data; boundary=-----------------------------1303767479498 -------------------------------1303767479498 Content-Disposition: form-data; name="foo" bar -------------------------------1303767479498 Content-Disposition: form-data; name="file1"; filename="me.jpg" Content-Type: image/jpeg PNG  ��� IHDR���������wSÞ���IDATc```����£ ã����IEND®B` -------------------------------1303767479498-- 
  • 写回答

1条回答 默认 最新

  • douzi9744 2011-04-26 14:48
    关注

    You have a couple of issues that I see.

    First, I am not able to successfully set the Content-Length header from script. I get error messages in Chrome when I attempt this. It looks like it is not necessary anyway, as the browser is setting the correct content-length for all of my test cases.

    Secondly, you are adding a Content-Type header, and you are putting a Content-Type faux-header into your request body:

    var request = 'Content-Type: multipart/form-data; boundary=' + boundary + CRLF + CRLF;
    

    That line should be removed. You are already setting the correct header. You cannot actually set headers in the body anyway. This is probably the source of your problem: this line is making your request body invalid, so of course the server cannot parse it.

    After some experimentation, I have developed a working example of how this might be done. I've tested this in Chrome latest, Firefox 4, IE9 and IE6. No libraries have been used, though of course, if you have them, it would greatly simplify the code.

    Please let me know if you find any issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?