2

我一直在尝试通过 PhoneGap (Cordova) 设置一个应用程序来拍摄图像并将它们上传到我们的服务器。我在这里浏览了很多回复并尝试了其中的代码。我可以拿起相机拍照,甚至可以访问手机图库。但我无法将图像发送到服务器。我试过发送图像,甚至发送 base64 图像流。我无法将它发送到服务器。

这是客户端的javascript:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

}

function ImageUpload() {
    this.useExistingPhoto = function(e) {
        this.capture(Camera.PictureSourceType.SAVEDPHOTOALBUM);
    }

    this.takePhoto = function(e) {
        this.capture(Camera.PictureSourceType.CAMERA);
    }

    this.capture = function(sourceType) {
        navigator.camera.getPicture(this.onCaptureSuccess, this.onCaptureFaile, {
            destinationType: Camera.DestinationType.FILE_URI,
            soureType: sourceType,
            correctOrientation: true
        });
    }

    this.onCaptureSuccess = function(imageURI) {
        var fail, ft, options, params, win;

        success = function(response) {
            alert("Your photo has been uploaded!");
        };

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

        options = new FailUploadOptions();
        options.fileKey = "file";
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
        options.mimeType = "text/plain";
        params = {
            val1: "some value",
            val2: "some other value"
        };
        options.params = params;
        ft= new FileTransfer();
        ft.upload(imageURI, 'http://style.appdev01.com/app/client-profile.php', success, faile, options);
    }

    this.OnCaptureFail = function(message) {
        alert("Failed because: "+message);
    }
};
var imageuploader = new ImageUpload();

点击时有两个按钮调用 imageuploader.takePhoto 和 .useExistingPhoto。

在服务器端我有这个 php: if(isset($_FILES['file'])) {

$target_path = "/home/style/public_html/images/client_images/app_image.jpg";

move_uploaded_file($_FILES['file']['tmp_name'], $target_path);

$insert = "INSERT INTO
    `fut`
SET
    `request` = '".serialize($_POST)."',
    `file` = '".serialize($_FILES)."'";
$mysql->query($insert);

}

这只是将 POST 和 FILE 数组存储到数据库,以确保它们通过并创建图像。

但同样,没有任何东西可以到达服务器。任何帮助将不胜感激。我已经从这里和整个网络上的很多问题中尝试了很多版本的代码。

4

2 回答 2

0
define ('SITE_ROOT', realpath(dirname(__FILE__))); /* echo SITE_ROOT; to dir
move_uploaded_file($_FILES["file"]["tmp_name"],SITE_ROOT."/uploads/".$_FILES["file"]["name"]); // will move file, make sure uplaods has write permission!

这适用于我在 Android 模拟器上,而不是在平板电脑上,但如果你有它工作,请告诉我,忙于同一件事。

$myarray = array( $_REQUEST);
foreach ($myarray as $key => $value) {

    echo "<p>".$key."</p>";
    echo "<p>".$value."</p>";
    echo "<hr />";
}

您可以使用它来检查 POST / GET!

于 2013-03-16T03:29:36.443 回答
0

试试这是我的代码。它对我有用。

  1. 通过 encodeURI 方法对您的 URL 进行编码
  2. 带有“文件”的文件密钥,如服务器端脚本 $_FILES['file']

    uploadFile: function(refNo){
    var uri = fileUpload.fileUri;
    var file = uri.substr(uri.lastIndexOf('/') + 1);
    
    var options = new FileUploadOptions();
    options.fileKey = "file"; 
    options.fileName = file;
    options.mimeType="image/jpeg";
    alert("name === "+uri);
    options.chunkedMode = false;
    
    
    var ft = new FileTransfer();
    Common.ajaxLoading('show');
    
    
    ft.upload(uri,encodeURI("http://172.16.1.147:80/upload/home.php") , fileUpload.uploadSuccess, fileUpload.uploadFail, options, true);
    },
    
于 2015-11-24T17:08:39.670 回答