2017-04-23 5 views
1

私は多くの記事を読んだが、私は最終回答を得ていない。 このlinkのコードから始めて、自分のファイルをアプリにダウンロードしました。とにかく、私は "ダウンロード"フォルダにそれを見たいと思います。 私はAndroidを使用していますが、明らかにiOS向けに有効なソリューションもあります。Cordova - ダウンロードフォルダにファイルをダウンロード

答えて

3

EDIT

あなたはすでにあなたはそれを移動することができ、ファイルのパスが分かっている場合:

var storageLocation = ""; 
 
console.log(device.platform); 
 
switch (device.platform) { 
 

 
    case "Android": 
 
     storageLocation = 'file:///storage/emulated/0/'; 
 
     break; 
 
    case "iOS": 
 
     storageLocation = cordova.file.documentsDirectory; 
 
     break; 
 

 
} 
 

 

 
var fileUri = "file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp‌​/test.pdf" 
 

 
function moveFile(fileUri) { 
 
    window.resolveLocalFileSystemURL(
 
      fileUri, 
 
      function(fileEntry){ 
 

 
       var parentEntry = storageLocation + "Download"; 
 
       
 
       // move the file to a new directory and rename it 
 
       fileEntry.moveTo(parentEntry, "newFile.pdf", success, fail); 
 
         
 
      }, 
 
      errorCallback); 
 
}

オリジナル

ここではサンプルです私が使うコードの一部これを実現する。それはAndroidで最もうまく動作します.iOSはアプリのサンドボックス化のために少し違うので、ファイルを自分で取得する必要があります。

var storageLocation = ""; 
 
console.log(device.platform); 
 
switch (device.platform) { 
 

 
    case "Android": 
 
     storageLocation = 'file:///storage/emulated/0/'; 
 
     break; 
 
    case "iOS": 
 
     storageLocation = cordova.file.documentsDirectory; 
 
     break; 
 

 
} 
 

 
window.resolveLocalFileSystemURL(storageLocation, 
 
    function (fileSystem) { 
 

 
     fileSystem.getDirectory('Download', { 
 
       create: true, 
 
       exclusive: false 
 
      }, 
 
      function (directory) { 
 

 
       //You need to put the name you would like to use for the file here. 
 
       directory.getFile("YOUR_FILE_NAME", { 
 
         create: true, 
 
         exclusive: false 
 
        }, 
 
        function (fileEntry) { 
 

 

 
         fileEntry.createWriter(function (writer) { 
 
          writer.onwriteend = function() { 
 
           console.log("File written to downloads") 
 
          }; 
 

 
          writer.seek(0); 
 
          writer.write(YOUR_FILE_HERE); //You need to put the file, blob or base64 representation here. 
 

 
         }, errorCallback); 
 
        }, errorCallback); 
 
      }, errorCallback); 
 
    }, errorCallback); 
 

 
var errorCallback = function(e) { 
 
    
 
    console.log("Error: " + e) 
 
    
 
}

その後のことができますディレクトリからファイルリストを取得するために:私はまた、私は、その後に合わせてストレージ・パスを変更することができ、アプリが実行されているどのデバイスを決定するためにコルドバデバイスプラグインを使用します使用:

window.resolveLocalFileSystemURL(storageLocation, 
 
    function (fileSystem) { 
 
    
 
     fileSystem.getDirectory('Download', { 
 
       create: true, 
 
       exclusive: false 
 
      }, 
 
      function (directory) { 
 

 
       var reader = directory.createReader(); 
 
       reader.readEntries(function (files) { 
 

 
        if (files.length == 0) { 
 

 
         console.log("No files found in downloads folder.") 
 

 
        } else { 
 

 
         $.each(files, function (i, v) { 
 

 
          console.log("File Name: " + files[i].name;) 
 

 
         }); 
 

 
        } 
 

 
       }, getFilesFail); 
 
      }, getFilesFail); 
 
    }, getFilesFail); 
 

 
var getFilesFail = function(e) { 
 
    
 
    console.log("Error: " + e); 
 
    
 
}

インストールするにはここ

cordova plugin add cordova-plugin-device 

ドキュメント:デバイスのプラグインは、このコマンドを使用し

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/

+0

ラインwriter.write(YOUR_FILE_HERE)のために、私はYOUR_FILE_HEREとしてファイルパスを使用することはできないと思う。実際に私は持っている:file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp/test.pdfしかし400kBの元のファイルはうまくコピーされていない(私は80Bのファイルをダウンロードフォルダ)。提案? – Zappescu

+0

私は公式のプラグインのdoc(https://github.com/apache/cordova-plugin-file-transfer#download-a-binary-file-to-the-application-cache-)で説明されているreadBinaryFileを使用しました。ファイルを入手しました。ちょうど最終的な答え:私はファイルマネージャーを使用してファイルにアクセスし、とにかく、Android 7.1(デスクトップのアイコン)の "公式"ダウンロードフォルダにダウンロードフォルダを見て、私はそれを見ることができません。どうして? – Zappescu

+0

ファイルをあるディレクトリから別のディレクトリに移動する関数を含めるように投稿を編集しました。 Androidダウンロードマネージャーによってそのフォルダにダウンロードされなかったため、公式ダウンロードアプリで見ることができない理由があると思います。 –

関連する問題