1

我正在使用 Sencha Touch 2 构建一个移动网络应用程序,我需要将文件从应用程序上传到 Amazon S3,并从 S3 获取文件 url。

PHP部分快完成了,我应该如何实现我的sencha程序以允许用户单击一个按钮,打开文件对话框,选择文件并调用PHP文件将文件上传到S3?

欢迎任何想法或建议!!!

4

1 回答 1

0

两天后终于弄明白了!

是的,无法使用 Sencha 访问 Android 文件系统和上传文件。

我放弃了使用Sencha的拍照功能,使用phonegap的拍照和文件传输功能,访问文件系统,然后上传到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);
        }

服务器端的 PHP 文件:

<?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";

}

?>

注意,上传的文件是没有扩展名的,所以我需要给它一个扩展名……关于S3的配置,请参考S3官网的secretkey……

于 2012-11-05T07:10:15.057 回答