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