Jump to content

Ajax POST with binary data


phpknight

Recommended Posts

I'm not entirely sure what you mean (only just started playing with AJAX this week)

but this might help, this is how I GOT an image (dynamically):

var image = NEW Image();

function data_string(data) { // Generates the binary data string from character / multibyte data
	var data_string='';
	for(var i=0,il=data.length;i<il;i++)data_string+=String.fromCharCode(data[i].charCodeAt(0)&0xff);
	return data_string;
}

function mime_from_data(data) { // Simple function that checks for JPG, GIF and PNG from image data. Otherwise returns false.
	if('GIF'==data.substr(0,3))return 'image/gif';
	else if('PNG'==data.substr(1,3))return 'image/png';
	else if('JFIF'==data.substr(6,4))return 'image/jpg';
	return false;
}


function display_image() {
	if (imageRequest.readyState == 4) {
		imageData = data_string(imageRequest.responseText); //RAW BINARY data of image, you know.. where it looks like diamonds with ?s in it
		var base64imageData = btoa(imageData);	
		var imageDataStream = 'data:' + mime_from_data(imageData) + ';base64,' + base64imageData;
		image.src = imageDataStream;
		image.name = "image"; //sets a name in as key so easy to find
		// !!!! NO, image.prototype.NEWTHING does _NOT_ work !!!! //
		document.images['image'].id = "Picture1";
		document.images['image'].width = "100";
		document.images['image'].height = "100";
		document.images['image'].border = "0";
		document.images['image'].alt = "OH LAWDY, AN IMAGE";
		document.images['image'].onmouseout = function () {alert("WHY DID YOU LEAVE ME?");};
		document.images['image'].onmousemove = function (event) {getMousePosition(event);}; //TAKE NOTE OF event!!
		/* OR you could use these two:
		graph.onmouseout = function () {alert("WHY DID YOU LEAVE ME?");};
		graph.onmousemove = function (event) {getMousePosition(event);};
		*/
		fs.appendChild(image);
	}
}

 

So I'm thinking, convert it to base64 then send it through php POST via an AJAX call

 

so basically send....

			imageData = from_form; //RAW BINARY data of image, you know.. where it looks like diamonds with ?s in it
		var base64imageData = btoa(imageData);	
		var imageDataStream = 'data:' + mime_from_data(imageData) + ';base64,' + base64imageData;
		//SEND THIS >>>>>>>> imageDataStream 

 

 

//edit

oh yeah... for future reference, if you ARE getting image data, imageRequest.overrideMimeType("text/plain; charset=x-user-defined");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.