使用phonegap将图像或视频上传到远程服务器

时间:2012-10-15 14:42:40

标签: javascript cordova

我在android phonegap做项目。我想将图片和视频上传到远程服务器。 我用了以下链接。 http://zacvineyard.com/blog/2011/03/upload-a-file-to-a-remote-server-with-phonegap

我还更改了一些选项,例如options.chunkedMode = false,android:debuggable =“true”和 。但它仍然显示错误代码3.我正在使用 cordova-2.0.0.js 版本。任何人都可以提出一些答案。

我的js代码是

    **

<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, "url of ther server/upload.php", win, fail, options, true);
                console.log("H");
    }

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

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

    </script>

**

我的php代码是

<?php

print_r($_FILES);
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_image_name);

?>

感谢。

1 个答案:

答案 0 :(得分:1)

您是否已将相关网址添加到白名单?

e.g。在config.xml中你有类似的东西:

<access origin="www.myurl.com" subdomains="true" />

<access origin="*" />

允许所有网址。

顺便说一下,错误代码3是FileTransferError.CONNECTION_ERR。