3

I'm making a Ionic app which has to download audio files stored in the user's Google Drive and store it in the Phone. Trying to get the file data and file write it in JavaScript but the file isn't working after i download it.

Things I've tried doing:

1. Sent a GET request and tried to file write the response and save the file

 var access_token = gapi.auth.getToken().access_token;

 $http({
   method:'GET',
      url:'https://www.googleapis.com/drive/v3/files/0B1ieItpgbVf8TFM2cC1MRHozekU?alt=media',
  headers:{'Authorization':'Bearer ' + access_token}
 }).success(function(result){
        // console.log(result);
     $scope.writefile(result);  
 }) 

$scope.writefile = function(funcdata) {

$scope.fle = funcdata;

document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("synksongs/sample6.mp3", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("Done Writing file");

    };
    writer.write($scope.fle);
}

function fail(error) {
    console.log(error.code);
}
}

ALthough the file is getting written i am unable to play it, seems to be corrupted or partially downloaded may be!

2.tried using this to retrieve the file content from the Drive and write the "ResponseText" to the file using my $scope.write() function:

 downloadFile(resp,function(result){
     $scope.writefile(result.responseText);         
 });

 function downloadFile(file,callback) {
  if (file.downloadUrl) {
    file.downloadUrl = file.downloadUrl+'&alt=media';
    console.log(file.downloadUrl);

    var accessToken = gapi.auth.getToken().access_token;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file.downloadUrl);
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xhr.onload = function() {
      callback(xhr);

    };
    xhr.onerror = function() {
      callback(null);

    };
    xhr.send();
  } else {

    callback(null);
  }
}

3. Tried using the ngcordova file transfer plugin but only a partial file gets downloaded

$scope.printFile = function() {
  var fileId = '0B1ieItpgbVf8TFM2cC1MRHozekU';

  var access_token = gapi.auth.getToken().access_token;

  var request = gapi.client.drive.files.get({
    'fileId': fileId,
    'headers': {
         'Authorization': 'Bearer ' + access_token,          
     }
  });

  request.execute(function(resp) {

      // console.log(resp);
     $scope.donwloadurl = resp.webContentLink;


  });
}

$cordovaFileTransfer.download($scope.donwloadurl, targetPath, options, trustHosts)
  .then(function(result) {
    // Success!
  }, function(err) {
    // Error
  }, function (progress) {
    $timeout(function () {
      $scope.downloadProgress = (progress.loaded / progress.total) * 100;
    });
  });

Can anybody help me with this! THanks!

  • Please have a look at this link - http://stackoverflow.com/questions/35503051/filetransfer-cordova-download-path/36152991#36152991 – Gandhi Apr 21 '16 at 07:57

0 Answers0