简体   繁体   English

Android将文件上传到网站扔了HTML和PHP

[英]Android upload file to website threw html and php

I'm getting a error when trying to upload a file to my website through android. 尝试通过android将文件上传到我的网站时出现错误。

Once I click on the photo i get this: 单击照片后,我得到以下信息: 在此处输入图片说明

this is my Main.java 这是我的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");
    }
}

This is my HTML file Where everything happens etc etc etc 这是我的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>

And here is my PHP file loacated on my website 这是我在网站上放置的PHP文件

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

Can anyone please give me a solution to this problem? 谁能给我解决这个问题的方法吗? Thanks! 谢谢!

remove this line from your PHP code 从您的PHP代码中删除此行

print_r($_FILES); print_r($ _ FILES);

If you wanted to return if the file upload was successful or not you could do something like 如果您想返回文件上传是否成功,则可以执行以下操作

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

echo ($fileUploaded) ? 'File uploaded' : 'Upload failed';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM