简体   繁体   English

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

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

I am trying to upload photo using cordova file transfer plugin. 我正在尝试使用cordova文件传输插件上传照片。 But is always returning Code 1 error which is FILE_NOT_FOUND error but the camera plugin is returning the imageURI perfectly OK. 但是总是返回Code 1错误,这是FILE_NOT_FOUND错误,但是相机插件完美返回了imageURI。 And I have tested my code in postman there it is working fine. 而且我已经在邮递员中测试了我的代码,效果很好。 But in javascript I am getting code 1 error. 但是在javascript中,我收到代码1错误。 I have also tried to hard-code the image URI but the error remains same. 我也尝试过对图像URI进行硬编码,但错误仍然存​​在。 Can anyone tell me what's the problem? 谁能告诉我这是什么问题? Here is the snippet of what is I am doing:- 这是我在做什么的摘要:

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

On implementation it is returning Code 1 error. 在实现时,它返回代码1错误。 I have run this in postman to check the URL but in postman it is working fine. 我已经在邮递员中运行此程序来检查URL,但在邮递员中它运行正常。

The imageURI is presumably "file:///data/data/..." - so the issue may be, that the folder is not accessible by FileTransfer. imageURI大概是“ file:/// data / data / ...”-这样的问题可能是FileTransfer无法访问该文件夹。

I had a similar problem, when trying to upload a photo from the photolibrary to social media. 尝试将照片从照相馆上传到社交媒体时,我遇到了类似的问题。 So i copied the file to the app's cache and uploaded the picture from there: 所以我将文件复制到应用程序的缓存中,然后从那里上传图片:

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

Try this code. 试试这个代码。 It working for me. 它为我工作。 You need to just call getImage function on button click or DeviceReady wherever you want to call. 您只需在要单击的位置单击按钮或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);
}

Here is a my PHP webservice that will store my file to the server. 这是我的PHP Web服务,它将我的文件存储到服务器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误代码1 Cordova插件文件传输Android - Error code 1 cordova plugin file transfer android Cordova文件传输插件下载总是产生http状态401和错误代码3 - Cordova file-transfer plugin download always yields http status 401 and error code 3 Cordova文件传输插件的IONIC错误 - IONIC Error with Cordova file-transfer plugin cordova-plugin-file-transfer:如何使用签名的URL将文件上传到S3? - cordova-plugin-file-transfer: How do you upload a file to S3 using a signed URL? 从cordova文件传输插件上传文件到nodeJS服务器 - upload file from cordova file transfer plugin to nodeJS server 我正在使用 react-native-photo-upload 库上传个人资料图片,但它抛出构建失败错误 - I'm using react-native-photo-upload library to upload profile pic but it's throwing build failed error 使用cordova插件文件传输下载进度事件在ios中不起作用 - Download progress event is not working in ios using cordova plugin file transfer 使用https的Cordova文件传输插件不适用于Windows - Cordova file transfer plugin using https not working for windows 如何使函数从cordova-plugin-file-transfer上传返回承诺 - How to make the function upload from cordova-plugin-file-transfer return a promise Cordova通过文件传输上传多个图像 - Cordova multiple image upload with File Transfer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM