【问题标题】:Ionic: Getting Error Code 3 when uploading image using ng-cordova fileTransfer and Camera pluginIonic:使用 ng-cordova fileTransfer 和 Camera 插件上传图像时出现错误代码 3
【发布时间】:2017-01-04 09:29:12
【问题描述】:

尝试使用 FileTransfer 插件将图像文件从我的 ionic 应用程序上传到远程服务器时出现“代码 3”(连接被拒绝)错误。

我使用了相机插件并将捕获的图像移动到永久存储中

$scope.selectPicture = function(sourceType) {
    var options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        correctOrientation:true
    };

    $cordovaCamera.getPicture(options).then(function(imagePath) {
        var currentName = imagePath.replace(/^.*[\\\/]/, '');
        //Create a new name for the photo
        var d = new Date(),
        n = d.getTime(),
        newFileName =  n + ".jpg";

        localStorage.setItem('checklist',newFileName);
        var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
          // Move the file to permanent storage
          $cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success){
            $scope.image = newFileName;
            localStorage.setItem('checklist',newFileName);
          }, function(error){
            $scope.showAlert('Error', error.exception);

          });
    }, function(err) {
      // error
    });
};

然后我使用 FileTransfer 插件上传图片

$scope.reportSending = function(){
    $scope.report_no = localStorage.getItem('reportNumber');
    $scope.imageLoc = localStorage.getItem('checklist');

    var server = "http://localhost/api/api/public/api/sendreport",

    filePath = cordova.file.dataDirectory + $scope.imageLoc;

    var date = new Date();

    var options = {
        fileKey: "file",
        fileName: $scope.imageLoc,
        chunkedMode: false,
        mimeType: "multipart/form-data",
        params : {
            report_no : $scope.report_no
        }
    };

    $cordovaFileTransfer.upload(server, filePath, options).then(function(result) {
        console.log(JSON.stringify(result.response));

    }, function(err) {
        console.log("ERROR: " + JSON.stringify(err));
        //alert(JSON.stringify(err));
    }, function (progress) {
        // constant progress updates
    });

};

当我执行reportSending() 函数时,它会返回一个错误:

ERROR: {"code":3,"source":"file:///data/user/0/com.ionicframework.appnew343084/files/1483519701226.jpg","target":"http://localhost/api/api/public/api/sendreport","http_status":null,"body":null,"exception":"Connection refused"}

它在异常中显示“连接被拒绝”,但是当我在邮递员中尝试 API 时,我可以成功上传文件。

【问题讨论】:

    标签: android angularjs cordova


    【解决方案1】:

    所以在搜索了大量的论坛后,我发现我的问题很简单..

    更改 API url 解决了这个问题。

    来自

    var server = "http://localhost/api/api/public/api/sendreport",
    

    到

    var server = "http://192.168.1.17/api/api/public/api/sendreport";
    

    我没有使用 localhost,而是将 URL 指向了我的本地服务器的 IP 而且我还注意到我在 API 的变量声明末尾使用了逗号 , 而不是分号。

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 2016-10-06
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多