2

尝试通过 android 将文件上传到我的网站时出现错误。

一旦我点击照片,我会得到这个: 在此处输入图像描述

这是我的 Main.java

    package test.com.phonegap.app;
import org.apache.cordova.DroidGap;
import android.os.Bundle;

public class Main extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}

这是我的 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() {

        }

        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, "http://monaiz.byethost7.com/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>

这是我网站上的 PHP 文件

    <?php
print_r($_FILES);
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/htdocs/".$new_image_name);
?>

谁能给我一个解决这个问题的方法?谢谢!

4

1 回答 1

0

从您的 PHP 代码中删除这一行

print_r($_FILES);

如果您想返回文件上传是否成功,您可以执行类似的操作

$fileUploaded = move_uploaded_file($_FILES["file"]["tmp_name"], "/htdocs/".$new_image_name);

echo ($fileUploaded) ? 'File uploaded' : 'Upload failed';
于 2012-09-14T16:37:49.440 回答