2016-08-26 3 views
0

Ich verwende die cordova-file-transfer-plugin und ich kann eine MP3-Datei perfekt auf Android herunterladen und abspielen, aber auf iOS existiert die Datei nicht.Wie kann ich mit Cordova auf eine heruntergeladene Datei im lokalen Dateisystem von iOS zugreifen?

Ich habe das Verzeichnis auf documentsDirectory geändert, aber wenn ich es in meinem Browser anrufe, funktioniert es nicht. Ich verwende das Codebeispiel, das auf dem Link angezeigt wird.

Ich habe auch this question betrachtet aber kein Glück.

var targetPath; 
        if (ionic.Platform.isIOS()) 
        { 
         targetPath = cordova.file.documentsDirectory + data.value.split('/').pop(); 
        } else 
        { 
         targetPath = cordova.file.dataDirectory + data.value.split('/').pop(); 
        } 
        $cordovaFileTransfer.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3', targetPath, {}, true) 
          .then(function (result) 
          { 
           console.log(result); 

          }, function (err) { 

           console.log(err); 
          }, function (progress) { 
           $timeout(function() { 
            console.log((progress.loaded/progress.total) * 100); 
           }); 
          }); 
+0

Könnten Sie einige Ihrer Code teilen? – Dexter

+0

gerade hinzugefügt. FYi es funktioniert gut in einem IOS-Simulator, aber nicht auf dem Gerät. –

+0

Hallo @duovili .. kann ich fragen, ob Ihre Download-Funktion keine Probleme hatte? Mine funktioniert, wenn ich die App nur neu starte .. –

Antwort

1

Hier ist Ihre Lösung. Sie benötigen das cordova cordovaFileOpener2-Plugin, um diese Datei zu öffnen. Hoffe du hast es schon installiert. Lass es mich wissen, es hat funktioniert oder nicht.

$scope.initializeDownload = function() { 
 
    var fileName = '0a5eabcc-231e-4831-b72f-c8980b78615c.mp3' 
 
    var storagePath = cordova.file.dataDirectory + fileName 
 
    window.resolveLocalFileSystemURL(storagePath, function success(fileEntry) { 
 
    fileEntry.file(function(file) { 
 
     $scope.downloadAttachment(file.localURL, options, trustHosts) 
 
    }) 
 
    }, function(err) { 
 
    window.resolveLocalFileSystemURL(cordova.file.documentsDirectory, 
 
     function success(dirEntry) { 
 
     dirEntry.getFile(fileName.replace(/^.*[\\\/]/, ''), { 
 
      create: true, 
 
      exclusive: false 
 
      }, 
 
      function(fileEntry) { 
 
      fileEntry.file(function(file) { 
 
       $scope.downloadAttachment(file.localURL, options, trustHosts) 
 
      }); 
 
      }, function(error) { 
 
      console.log(error) 
 
      }); 
 
     }, function(error) { 
 
     console.log(error) 
 
     }); 
 
    }); 
 
} 
 

 

 
$scope.downloadAttachment = function(fileURL, trustHosts, options) { 
 
    var ft = new FileTransfer(); 
 
    ft.onprogress = function(progress) { 
 
    // Here you can get your download status 
 
    //var downloadStatus = (progress.loaded/parseInt(fileSize)) * 100; 
 
    //console.log(downloadStatus) 
 
    }; 
 
    ft.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3', 
 
    fileURL, 
 
    function(result) { 
 
//Second argument is the file Mime type 
 
     $cordovaFileOpener2.open(result.nativeURL, 'audio/mpeg').then(function(result) { 
 
     console.log("Open success") 
 
     }, function(err) { 
 
     console.log("Open Error") 
 
     }); 
 
    }, 
 
    function(error) { 
 
     ft.abort(); 
 
     console.log(error.exception || error.body) 
 
    }, 
 
    trustHosts, 
 
    options 
 
); 
 
}

+0

aber öffnet dieses Plugin es nicht im nativen MP3-Player? Ich möchte es in der App mit dem HTML-Player in der App öffnen. –

+0

Welche Erfahrung bekommen Sie gerade? Was auch immer ich von cordova gelernt habe, es gibt Option Benutzer, App zu wählen, um jede Datei zu öffnen. –

+0

Auf Android-Gerät und IOS-Simulator ruft die Datei kein Problem auf (mit absoluter Datei-URL) und spielt es ab, aber auf einem IOS-Gerät in der Konsole heißt es nur: "Es ist ein Fehler beim Abrufen der Ressource aufgetreten", und das weiß ich genau Die Ressource ist da. –

Verwandte Themen