Phonegap:上传失败

时间:2014-01-01 16:09:24

标签: android cordova upload photo

  

Phonegap:2.9.0

     

Android:4.4.2

     

设备:Nexus 5

我将“assets \ www”文件移动到我的主机,并且“super.loadUrl(”http://mydomain.com“);”,因为它很容易维护,幸运的是,Phonegap有效!

但它无法上传文件,为什么?这是我的代码:

MainActivity.java

import android.os.Bundle;
import org.apache.cordova.*;

public class MainActivity extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.loadUrl("http://mydomain.com");
    }
}

index.php(在主持人上)

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
function onDeviceReady()
{
    console.log("device ready");
    // 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, "upload.php", win, fail, options);
}

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

function fail(error)
{
    alert("An error has occurred: Code = " + error.code);
}
</script>
</head>

<body>
<button onclick="location.replace(location.href);">Refresh</button>
<button onclick="getImage();">Upload a Photo</button>
</body>
</html>

</html>

upload.php(在主机上)

<?php
print_r($_FILES);
?>

我单击按钮并选择一张照片,它会发出警告“发生错误。编码= 2”,如何解决这个问题?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要指定upload.php文件的完整路径,例如:

    ft.upload(imageURI, "http://www.mydomain.com/upload.php", win, fail, options);