How to send base64 decoded data through ajax to php file - php

I want to send base64 decoded data to php file from ajax, but my half of the data get truncated, not able to send entire data through ajax to php file.
I am sending this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAgAElEQ…/Hv5‌​9f3b//PQ+Q/3vcf35r/vmLQP0RPc/m74Zy4dP//wBHqel8UTBFDwAAAABJRU5ErkJggg==
and converting the base64 image to normal image like jpg
Any Help ?

Just a guess. Don't hurt me Stackoverflow gods. :-) Use JSON, not urlencoding. Make an object literal and then put your data inside. Send the data your PHP file and use json_decode.
var image {image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAgAElEQ…/Hv5‌​9f3b//PQ+Q/3vcf35r/vmLQP0RPc/m74Zy4dP//wBHqel8UTBFDwAAAABJRU5ErkJggg=="};

Related

Postman upload file on API using raw

How do you upload a file on postman using raw mode.
I used a json entry like this:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"#/User/images.jpg"
}
the problem was the # in php was deprecated and now recommended to use curlFile, how to do it in a json form?
Thanks.
Since you are sending the image as part of the JSON, you would need to encode it. Base64 encoding is a good choice. On the server side, you will need to decode it as well.
See this answer to How to convert an image to base64 encoding?:
$imagedata = file_get_contents("/path/to/image.jpg");
$base64 = base64_encode($imagedata);
Once you have the data, you can add it to the JSON:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"TWFuIGlzIGRpc3R..."
}
Also see Base64 Reference

WinINET and PHP php://input POST read

I have a weird problem. I want to upload some data with WinInet to a PHP script.
When I upload the data at once with HttpSendRequest(), then PHP reads the uploaded data correctly, e.g.
$entityBody = file_get_contents('php://input');
When I upload the data in parts with HttpSendRequestEx() and InternetWriteFile() , then the same data is uploaded, but PHP fails to read the input (empty).
What could be wrong?
Is the PHP script "called" before the entire data is uploaded?
If so, how do I get the data?
Found it, Content-Length header was missing.

Create Base64 from uploaded file without saving locally php

I need to send a file in base 64 format through a SOAP service. I cannot save the file locally. Is there a way to convert an uploaded file and send it through in one instance, without saving it?
I initially thought it was as easy as:
$base64file = base64_encode($_FILES["cv"]["tmp_name"]);
But that doesn't seem to be working great.
The problem with your code is that you are encoding the filename, not the file content.
Use this to open the file and convert it to base64:
$base64file = base64_encode(file_get_contents($_FILES["cv"]["tmp_name"]));
Then you can send it back to the client. Just double check that the Soap server does not double base64 encode the string.

Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.

PHP: How can I make a webservice that respondes with XML and an image?

My PHP abilities are almost non-existent and I am trying to make a very simple REST webservice. The service at present queries a database and returns an image in a 'application/octet-stream' response. I am calling this webservice via ASIHTTP (a REST iPhone framework) which is returning the image perfectly fine :)
Is there any way to make the PHP service return an image AND an XML file? I am thinking the only way to do this is to write the image byte array directly into the XML file. If so - how do I do this with PHP?
Thank you
If ASIHTTP supports this, you could try embedding the image data in the XML, but you'd have to read the relevant documentation first. You can't simply dump a bunch of binary data into an XML file, you need to convert it to something like base64 first.
IMO, a better (more robust) approach is to send both files independently: make one request for the XML, which may contain an ID or something for the image, and then another request to get the image itself.
Apart from more robust code, you will also be able to parse the entire XML before the image is fully loaded. Seeing how images are typically much larger than XML messages, the difference is going to be noticable.
I think the only way would be to base64 encode your image data with PHP and decode it on your iPhone. See the base64_encode manual page - the first comment describes how to use it to read an image.
One method would be to take the binary data and (for example) base64_encode it, embed that in your XML, and then when you receive it you can base64_decode it using + (NSData *)decodeBase64WithString:(NSString *)strBase64 { from here:
NSData *data = [Base64 decodeBase64WithString:strBase64];
UIImage *image = [UIImage imageWithData:data];

Categories