Cordova文件传输插件

时间:2017-02-27 05:06:34

标签: angularjs cordova ionic-framework

我正在创建离子应用程序,我想下载一个动态的图像,并使用cordova文件传输插件,但它无法正常检查我在哪里做错了。

  

错误: - 未定义网址,无法读取属性'拆分'未定义的

  $

scope.downloadImage = function() {
            $http.get('http://sabkideal.com/phpapi_/cashback.php').success(function(response) {
      $scope.data = response;

       for (var i=0 ;i <response.length; i++)
       {
        var url = response[i].image;
        var deal = response[i].id;
        //url showing the same url every time i click and not jumping to next statement when click on send image download .
       console.log(deal);

      console.log(url);

      var filename = url.split("/").pop  ;

 console.log(filename);

      var     targetPath = encodeURI(cordova.file.dataDirectory + fileName);
       console.log(targetPath);

    var   options = {};
       var  trustHosts = true;

        }


                $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
      .then(
        function(result) {
          alert('Download success');
          refreshMedia.refresh(targetPath);
        },
        function(err) {
          alert('Error: ' + JSON.stringify(err));
        },
        function(progress) {
          // progressing download...
        })

});


     } 

1 个答案:

答案 0 :(得分:0)

确保数据返回未定义。通过实施检查,您可以检查数据是否未定义。然后使用而不是成功,因为它在Angular 1.5及以下

中已弃用
 $http.get('http://sabkideal.com/phpapi_/cashback.php').then(function(response) {
      $scope.data = response.data;

       // Need to check response data so that it is not undefined
       if ($scope.data !== undefined && $scope.data !== null && $scope.data !=="") {
         for (var i=0 ;i <$scope.data.length; i++)
         {
              // Checking also need to be done here
             if ($scope.data[i].image !== undefined && $scope.data[i].image 
                                        !==null && $scope.data[i].image !== "")  {
                 var url = $scope.data[i].image;                    
                 var filename = url.split("/").pop  ;
             }