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

In cordova project I am using file transfer plugin to upload a profile photo it always returns Code 1 error?

本文关键字:个人资料 照片 返回 错误 代码 插件 项目 cordova 传输 文件      更新时间:2023-09-26

我正在尝试使用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);
        }

在实现时,它返回代码 1 错误。我已经在邮递员中运行了这个来检查 URL,但在邮递员中它工作正常。

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");
}

试试这段代码。它对我有用。您只需在单击按钮时调用getImage函数,或者在您想要调用的任何地方调用DeviceReady。

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网络服务,它将我的文件存储到服务器。

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