1

我在 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);

?>

谢谢。

4

1 回答 1

1

您是否已将相关 URL 添加到白名单中?

例如在 config.xml 你有类似的东西:

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

或者

<access origin="*" />

允许所有 URL。

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

于 2012-10-15T15:01:59.190 回答