Phonegap:照片无法上传

时间:2012-03-26 16:41:12

标签: api cordova upload camera photo

我正在尝试创建一个非常基本的照片上传功能。我在线上学了很多教程和例子,但实际上我遇到了一些问题。

目前我收到以下错误:

2012-03-26 17:37:02.107 Upload[84710:13403] fileData length: 72154
2012-03-26 17:37:02.119 Upload[84710:13403] File Transfer Error: unsupported URL
2012-03-26 17:37:02.121 Upload[84710:13403] [INFO] Error in error callback: org.apache.cordova.filetransfer1 = ReferenceError: Left side of assignment is not a reference.

这是我的Phonegap HTML文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>File Transfer Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for PhoneGap to load
        document.addEventListener("deviceready", onDeviceReady, false);

        // PhoneGap is ready
        function onDeviceReady() {
  // Do cool things here...
        }

        function getImage() {
            // Retrieve image file location from specified source
            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) {
            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;
            options.chunkedMode = false;

            var ft = new FileTransfer();
            ft.upload(imageURI, "hosting.domain.co.uk/ios/upload.php", win, fail, options);
        }

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

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

        </script>
</head>
<body>
    <button onclick="getImage();">Upload a Photo</button>
</body>
</html>

这是我的upload.php文件:

<?php
print_r($_FILES);
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/vhosts/domain.co.uk/subdomains/hosting/httpdocs/ios/uploads".$new_image_name);
?>

有什么明显的我做错了吗?

非常感谢

乔恩

1 个答案:

答案 0 :(得分:4)

您正在使用fileURI上传照片,这就是您收到不受支持的文件格式错误的原因。这是因为fileURI的格式为file://localhost/xx1.jpg。服务器端的上传器功能不支持此功能。

Javascript无法访问原生文件系统

Javascript无法直接访问底层文件系统,因此如果是phonegap,则无法通过文件传输进行上传。

解决方案

您最好的选择是上传图像的base64编码字符串。您可以使用navigator.camera.DestinationType.DATA_URL选项destinationType获取base64编码图像。这将返回base64编码的字符串。这可以很容易地通过json发送到服务器,并带有相关的元数据,如jpg或png格式和文件名等。

看看here成功使用手机版和imgur提供的在线图片托管设施。

祝你好运和干杯