使用ajax

时间:2016-07-14 08:03:42

标签: javascript android jquery ajax cordova

我正在使用phonegap开发应用程序, 我的应用程序,从相机或图库捕获图像,我移动到一个特定的文件夹,然后我想将其上传到服务器。我也从表单上传了一些数据。 我的问题是,如果我使用画廊中的图像,一切都有效,但如果我从相机捕获它,它就无法工作。这是我的代码js。

var pictureSource;   // picture source
var destinationType; // sets the format of returned value
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageURI) {

var Image = document.getElementById('Image');
var form = document.getElementById('up');
var b1 = document.getElementById('b1');
var b2 = document.getElementById('b2');
Image.style.display = 'block';
form.style.display = 'block';
b1.style.display = 'none';
b2.style.display = 'none';
Image.src = imageURI;
movePic(imageURI);
}

function capturePhoto() {
// Take picture using device camera and retrieve image 
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
});
}

function getPhoto(source) {
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
    destinationType: destinationType.FILE_URI,
    sourceType: source    });
}

function onFail(message) {
alert('Failed because: ' + message);
}

/*****************save*****************/

function movePic(file){
  window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError);
}
//Callback function when the file system uri has been resolved
function resolveOnSuccess(entry){
var d = new Date();
var n = d.getTime();
var newFileName = n + ".jpg";
var myFolderApp = "folder";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
    //creo la cartella geofolder e salvo le foto scattate
   fileSys.root.getDirectory(myFolderApp,
            {create:true, exclusive: false},
            function(myFolderApp) {
                entry.moveTo(myFolderApp, newFileName,  successMove,                                            resOnError);
                document.forms['up'].foto.value = newFileName;

            },
            resOnError);
    },
    resOnError);
return newFileName;
}

//Callback function when the file has been moved successfully 
function successMove(entry) {
//Store imagepath in session for future use
// like to store it in database
sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError() {
alert(error.code);
}

////upload///

$("#upload").click(function(){
    //some variable

    uploadimage(foto);
    $.ajax({
        type: "POST",
        url: "http://x.org/upload.php",
        data: {//some data from the form},
        success: function(responce){
            if(responce=='success')
            {
            alert('ok');
            }
            else
            {
            alert("errore");

            }
        }
    });
    return false;});

function uploadimage(foto) {

var Image = document.getElementById('Image').getAttribute("src");

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=foto;
options.mimeType="image/jpeg";

var params = new Object();
params.nome = "test";

options.params = params;
options.chunkedMode = false;

var ft = new FileTransfer();
ft.upload(Image, "http://x.org/uploadphoto.php", win,    fail, options);
}

function win() {
alert("ok bro");
}
function fail() {
alert("not bro");

}

1 个答案:

答案 0 :(得分:1)

问题出在movePic()函数:

您正在尝试上传因移动而不再存在的uri!

尝试entry.copyTo()  而不是

entry.moveTo();