0

我的html代码

<!DOCTYPE html>
<html>
<head>
    <title>Login Page</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">

 <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript">


    function PictureSourceType() {};
    PictureSourceType.PHOTO_LIBRARY = 0;
    PictureSourceType.CAMERA = 1;

    function getPicture(sourceType)
    {
         var options = { quality: 10 };
         if (sourceType != undefined) {
              options["sourceType"] = sourceType;
             // options["destinationType"] = destinationType.DATA_URL;
         }
         // if no sourceType specified, the default is CAMERA 
         navigator.camera.getPicture(getPicture_Success, null, options);
    };


    function getPicture_Success(imageData)
    {
             alert("getpic success "+ imageData);
            document.getElementById("test_img").src =  imageData;
    };

    function success(response) {
        alert("Your photo has been uploaded!");
      };
      // callback if the photo fails to upload successfully.

      function fail(error) {
        alert("if refreshed An error has occurred: Code = " + error.code);
      };

    function uploadPhoto()
    {
        var imageFile = document.getElementById("test_img").src;
        alert(imageFile);




        var ft,options;


        options = new FileUploadOptions();
        options.fileKey = "profile_image";
          // name of the file:
          options.fileName = imageFile.substr(imageFile.lastIndexOf('/') + 1);
          // mime type:
          options.mimeType = "multipart/form-data";
          params = {
            val1: "some value",
            val2: "some other value"
          };
          options.params = params;



        ft = new FileTransfer();
        ft.upload(imageFile, 'http://192.168.123.199/saveImage.php', success, fail, options,false);
        alert("There is something called file transfer " + imageFile);

    };

   </script>
    </head>

     <body>
 <div data-role="page" id="cameraPage">
<div data-role="header" data-position="inline">
    <h1>Edit profile Pic</h1>
    <a href="index.html" data-icon="delete" class="ui-btn-right">Cancel</a>
</div>

<div data-role="content">
    <center>
    <img style="width: 60px; height: 60px" id="test_img" src="" />
    </center>

    <button onclick="getPicture()">From Camera</button>

    <button onclick="getPicture(PictureSourceType.PHOTO_LIBRARY)">From
        Photo Library</button>
    <button onclick="uploadPhoto()">Upload Photo</button>


</div>

我的PHP代码

<?php// Directory where uploaded images are saved

$target_path = "tmp/";

$target_path = $target_path . basename( $_FILES['profile_image']['name']); 

if(move_uploaded_file($_FILES['profile_image']['name'], $target_path)) {
echo "The file ".  basename( $_FILES['profile_image']['name']). 
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

在日志猫

08-02 23:21:05.835: D/FileTransfer(16094): upload content://media/external/images/media/1889 to http://192.168.123.199/saveImage.php
08-02 23:21:05.835: D/FileTransfer(16094): fileKey: profile_image
08-02 23:21:05.835: D/FileTransfer(16094): fileName: 1889
08-02 23:21:05.835: D/FileTransfer(16094): mimeType: multipart/form-data
08-02 23:21:05.835: D/FileTransfer(16094): params: {"val1":"some value","val2":"some other value"}
08-02 23:21:05.835: D/FileTransfer(16094): trustEveryone: false
08-02 23:21:05.835: D/FileTransfer(16094): chunkedMode: true
08-02 23:21:08.740: D/dalvikvm(16094): GC_CONCURRENT freed 252K, 48% free 3122K/5959K, external 882K/1314K, paused 8ms+14ms

我看不到上传成功或错误的警报

4

1 回答 1

1

我使用以下php代码保存上传的文件

<?php
$target_path = "tmp/";
if ($_FILES["profile_image"]["error"] > 0){
    echo "Error: " . $_FILES["profile_image"]["error"] . "<br>";
}
else{ 
    if (file_exists($target_path . $_FILES["profile_image"]["name"])){
        echo $_FILES["profile_image"]["name"] . " already exists. ";
    }
    else{
        move_uploaded_file($_FILES["profile_image"]["tmp_name"], $target_path . $_FILES["profile_image"]["name"]);
        echo "Stored in: " . $target_path . $_FILES["profile_image"]["name"];
    }
}
?>
于 2013-07-31T06:35:13.930 回答