Search code examples
phpjavascriptmobile-website

Forced download of image raw data is corrupted on android 2.3


I'm trying to develop a mechanism to download a canvas as an image by forcing download via php. the following code works in chrome desktop but NOT on an Android 2.3 stock browser.

HTML & JavaScript:

<script> function downloadImage()
{

    document.getElementById('action').value = 'downloadfile';
    document.getElementById('source').value = document.getElementById('finalimage').src;
    document.getElementById('user').value = userId;
    document.getElementById('imageForm').submit();
}
</script>
<html>
    <form id="imageForm" action="" method="post" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="source" id="source" value=""/>
    <input type="hidden" name="action" id="action" value="savefile"/>
     <input type="hidden" name="user" id="user" value=""/>
    </form>
</html>

PHP Code:

<?php

header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"colouringbook-page.jpg\"");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    header("Content-transfer-encoding: binary");

        $source = $_REQUEST['source'];
        $source = base64_decode(substr($source, strpos($source, ",")+1));
        header("Content-Length: " . strlen($source));
        print $source;
        exit;
?>

The problem I am getting is firstly a "Download unsuccessful". Then when I DO manage to get a successful download the image is corrupted.

[Update]

I am testing by just outputting to browser then trying to save the image from there. I'm getting the image displayed but in saving the image the file naming scheme is *.html

Looks like it can be an android stock browser bug. I was able to save the image to the server but still couldn't output the buffer from there to download the image. I had to output a regular html tag then do a "save as" on the android to download the image.


Solution

  • Whats seems to be happening is that the Android device is attempting to "re-request" the process which is POSTED logic process. Apparently the device download manager doesn't POST anything.

    The solution I came up with was to use a session to store the information