在cordova项目中我使用文件传输插件上传个人资料照片它总是返回Code 1错误?

时间:2016-03-16 05:36:16

标签: javascript android cordova file-transfer

我正在尝试使用cordova文件传输插件上传照片。但总是返回代码1错误,这是FILE_NOT_FOUND错误,但相机插件正在返回imageURI完全正常。我已经在邮递员那里测试了我的代码,它运行正常。但在JavaScript中我得到代码1错误。我也尝试对图像URI进行硬编码,但错误仍然相同。谁能告诉我这是什么问题? 以下是我正在做的事情的片段: -

navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
            mediaType: Camera.MediaType.PICTURE,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY
        });

        function  onPhotoURISuccess(imageURI) {
            alert(imageURI);

            var options = new FileUploadOptions();
            options.fileKey="file";
            //options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.fileName="image.jpeg";
            options.mimeType="image/jpeg";
            options.httpMethod="PUT";
            options.trustAllHosts=true;
            options.chunkedMode=false;


             var header ={};
            //header.ContentType="image/jpeg";
            header.Connection="close";  
            //header.Authorization="Basic c2FoaWwuc2V0aGk6V2VsY29tZUAwNw==";
            options.headers =header;

            var ft = new FileTransfer();                               
            ft.upload(imageURI,encodeURI("servername.com"/profiles/photo.do?
            key="hrhgfhjf23435"),win,fail,options);

            function win(r) {
                alert("file uploaded");
                console.log("Code = " + r.responseCode);
            }

            function fail(error) {
                alert("An error has occurred: Code = " + error.code);
                alert("upload error source " + error.source);
                alert("upload error target " + error.target);
            }
        }
        function onFail(message) {
            //Uncomment to view the image file URI
            alert("in fail");
            alert(message);
        }

在实现时,它返回Code 1错误。我在邮递员中运行这个以检查URL,但在邮递员中它运行正常。

2 个答案:

答案 0 :(得分:0)

imageURI可能是“file:/// data / data / ...” - 所以问题可能是,FileTransfer无法访问该文件夹。

当我试图将照片从照片库上传到社交媒体时,我遇到了类似的问题。所以我将文件复制到应用程序的缓存中并从那里上传了图片:

moveFile($scope.imageURI);

        function moveFile(fileUri) {
          window.resolveLocalFileSystemURL(
            fileUri,
            function(fileEntry){
              window.resolveLocalFileSystemURL(
                cordova.file.externalCacheDirectory,
                function(dirEntry) {
                  fileEntry.copyTo(dirEntry, fileEntry.name, onSuccess(fileEntry, dirEntry), onFail);
                },
                onFail);
            },
            onFail);
        }

function onSuccess(fileEntry, dirEntry) {
          $scope.imageURI = dirEntry.nativeURL + fileEntry.name;
        }

function onFail() {
   alert("error copying picture");
}

答案 1 :(得分:0)

试试这段代码。它为我工作。您只需要在按钮单击或DeviceReady上调用getImage函数,无论您想要调用它。

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();
    //YOUR_URL = Actual web url where web-service is exists.
    ft.upload(imageURI, "YOUR_URL", 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);
}

这是我的PHP Web服务,它将我的文件存储到服务器。

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