$ cordovaFileTransfer无法正常工作

时间:2017-05-23 08:12:41

标签: angularjs cordova ionic-framework

使用$ cordovaFileTransfer上传图片,但它停止在99%,$ _FILES在PHP中为空; 我得到了evt对象。

{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"16656","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"33040","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"98576","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"131344","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"147728","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"164112","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"180496","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"213264","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"229648","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"65808","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"82192","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"114960","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"295184","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"262416","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"311568","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"327952","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"344336","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"360720","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"377104","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"409872","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"442640","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"393488","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"426256","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"459024","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"475408","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"491163","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"196880","total":"491176"}
{"bubbles":"false","cancelBubble":"false","cancelable":"false","lengthComputable":"true","loaded":"246032","total":"491176"}

加载的属性有什么问题;为什么它会重复增加;

上传代码

$scope.upload = function(imageURI) {
    $scope.dangerList[$scope.setting.num][$scope.setting.imageType + '1pic'] = imageURI;
    var server = 'http://localhost/test.php';
    var dirName = 'check';
    var desName = 'test';
    var options = {
        'httpMethod': 'POST',
        'params': {
            'dirName': dirName,
            'desName': desName
        }
    };
    var promise = $cordovaFileTransfer.upload(server, imageURI, options, true);
    promise.then(function(data) {
        console.log(data);
    }, function(data) {}, function(evt) {
        $ionicLoading.show({
            template: '<p>upload:' + parseInt(100.0 * evt.loaded / evt.total) + '%</p>',
            //duration: 1000,
        });
    });
    return promise;
};

和ngCordova \ src \ plugins \ fileTransfer.js

angular.module('ngCordova.plugins.fileTransfer', [])

.factory('$cordovaFileTransfer', ['$q', '$timeout', function($q, $timeout) {
    return {
        download: function(source, filePath, options, trustAllHosts) {
            var q = $q.defer();
            var ft = new FileTransfer();
            var uri = (options && options.encodeURI === false) ? source : encodeURI(source);

            if (options && options.timeout !== undefined && options.timeout !== null) {
                $timeout(function() {
                    ft.abort();
                }, options.timeout);
                options.timeout = null;
            }

            ft.onprogress = function(progress) {
                q.notify(progress);
            };

            q.promise.abort = function() {
                ft.abort();
            };

            ft.download(uri, filePath, q.resolve, q.reject, trustAllHosts, options);
            return q.promise;
        },

        upload: function(server, filePath, options, trustAllHosts) {
            var q = $q.defer();
            var ft = new FileTransfer();
            var uri = (options && options.encodeURI === false) ? server : encodeURI(server);

            if (options && options.timeout !== undefined && options.timeout !== null) {
                $timeout(function() {
                    ft.abort();
                }, options.timeout);
                options.timeout = null;
            }

            ft.onprogress = function(progress) {
                q.notify(progress);
            };

            q.promise.abort = function() {
                ft.abort();
            };

            ft.upload(filePath, uri, q.resolve, q.reject, options, trustAllHosts);
            return q.promise;
        }
    };
}]);

并推送拦截器

.factory('UserInterceptor', function ($q, $rootScope) {
    return {
        request:function(config){
            config.headers = config.headers || {};
            config.headers.UID = $rootScope.user.id || 0;
            return config;
        },
        requestError: function(err){
            return $q.reject(err);
        },
        response: function (response) {
            return response;
        },
    };
})

它可以在几天前工作。 在此期间,

  1. 使用android@5.1.1而不是android@4.1.1添加平台;
  2. update cordova;
  3. 现在使用离子1.7.3;
  4. ,这是下载代码,它可以正常工作,但会下载文件两次

    $scope.down = function(fname) {
        var fileTransfer = new FileTransfer();
        var uri = encodeURI($rootScope.rootUrl + fname);
        var fileURL = cordova.file.externalRootDirectory + fname;
        fileTransfer.download(
            uri,
            fileURL,
            function(entry) {
                console.log(entry);
            },
            function(error) {
                console.log(error);
            },
            false, {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
        );
    };
    

1 个答案:

答案 0 :(得分:0)

这实际上是来自cordovaFileTransfer的进度事件的响应。 成功后,它会给出这样的回应:

Object {bytesSent: 3228135, responseCode: 200, response: "<!-- Served from 80.251.0.59 / test.talia.net, as …imited↵</p>↵↵</td></tr></table>↵↵</body>↵</html>↵", objectId: ""}

但是根据你得到的输出,我认为该函数被来自另一个实例的相同方法中断,因为你的整个文件几乎上传到第3行的最后一行。

"cancelable":"false","lengthComputable":"true","loaded":"491163","total":"491176"}

然后它又从另一点上升了。 请提供代码,以便我可以帮助您进一步。 谢谢