Phonegap上传图片到服务器无法正常工作

时间:2017-07-25 07:18:14

标签: javascript php cordova file-upload phonegap

我尝试将图片从Android平板电脑上传到在线服务器。但是,它不起作用并返回错误代码1(警告:发生错误:代码= 1)。有人解决了这个问题吗?

这是我的js文件

function uploadFromGallery() {

    // 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)+'.jpeg';
    options.mimeType="image/jpeg";

    var params = new Object();

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI("http://serverurl/uploadImage.php"), win, fail, options);
}

function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

这是uploadImage.php

<?php 
 // Directory where uploaded images are saved
 $dirname = "image"; 
 // If uploading file
 if ($_FILES) {
    print_r($_FILES);
    mkdir ($dirname, 0777, true);
    move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
 }
?>

0 个答案:

没有答案