1

I am building a mobile web app using Sencha Touch 2, and I need to upload file from the app to Amazon S3, and get the file url from S3.

The PHP part is almost finished, how shall I implement my sencha program to allow user click a button, and open the file dialog, select the file and call the PHP file to upload the file to S3?

Any idea or suggestion is welcomed!!!

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
zhoubo
  • 39
  • 8

1 Answers1

0

Finally figured it out after two days!

Yes, it is impossible to use Sencha to access Android file system and upload files.

I gave up useing Sencha's camera function ,and used phonegap's camera and filetransfer function, access the file system, then upload to S3.

console.log("camera should be called!"); 
        navigator.camera.getPicture(uploadPhoto,
                function(message) { alert('get picture failed'); },
                { quality: 50, 
                destinationType: navigator.camera.DestinationType.FILE_URI,
                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
                );
        function uploadPhoto(imageURI) {
            console.log(imageURI);
            var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";

            var params = new Object();
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;


            var ft = new FileTransfer();
            ft.upload(imageURI, "http://54.251.39.219/index.php", win, fail, options);
        }

        function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
        }

        function fail(error) {
            alert("An error has occurred: Code = " = error.code);
        }

The PHP file on server side:

<?php
include('image_check.php');
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
echo '<b>S3 File URL:</b>';
$name = $_FILES['file']['name'];
$name = $name . '.jpg';
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);

if(strlen($name) > 0)
{
echo "strlen ok";
echo "filename is ".$name;
echo "extension is ".$ext;
if(in_array($ext,$valid_formats))
{

if($size<(1024*1024))
{
include('s3_config.php');
//Rename image name. 
$actual_image_name = time().".".$ext;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, 

S3::ACL_PUBLIC_READ) )
{
$msg = "S3 Upload Successful.";
echo 'upload successful!';  
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
echo "<img src='$s3file' style='max-width:400px'/><br/>";
echo '<b>S3 File URL:</b>'.$s3file;

}
else
echo "S3 Upload Fail.";

}
else
echo "Image size Max 1 MB";

}
else
echo "Invalid file, please upload image file.";
}
else
echo "Please select image file";

}

?>

Note, the file uploaded is without extension, so I need to give it an extension... For the S3 configuration, please refer to the S3 official website for things like secretkey...

zhoubo
  • 39
  • 8