2017-06-04 2 views
0

私は、Windows上でオクテットストリームとしてjpgを保存すると、その後の読み取りでサイズが0のファイルが返されるという問題が発生しています。私のハードドライブ、およびファイルが正常に見えますが、ファイルの読み取りに失敗します。Cordova FileEntry.file()size 0を返す

以下のコードは、私がファイルシステムとやりとりするために使用しているものです。この問題を解決するためにいくつかのハックが投げ込まれています。

function downloadFileFromUrl(url, localPath, contentType, successCallback, failureCallback) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open('GET', url, true); 
    xhr.responseType = 'blob'; 

    xhr.onload = function() { 
     if (this.status == 200) { 
      localPath = encodeURI(localPath); 
      writePersistantFile(localPath, this.response, true, contentType, 
       function (fileEntry) { 
        if (successCallback != undefined) { 
         successCallback(fileEntry); 
        } 
       }, 
       function (error) { 
        if (failureCallback != undefined) { 
         failureCallback(error); 
        } 
       }); 
     } 
    }; 
    xhr.send(); 
} 

function createDirectoryRecursively(path, parentDir, callback, errorCallback) { 
    if (path == null || path.length == 0) { 
     callback(parentDir); 
     return; 
    } 
    var index = path.indexOf('/'); 
    var dirName = null; 
    var remainder = null; 
    if (index <= 0) { 
     dirName = path; 
    } else { 
     dirName = path.substring(0, index); 
     remainder = path.substring(index + 1); 
    } 

    parentDir.getDirectory(dirName, { create: true, exclusive: true }, function (dirEntry) { 
     self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback); 
    }, function (error) { 
     if (error.code == 12) { 
      parentDir.getDirectory(dirName, { create: false }, function (dirEntry) { 
       self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback); 
      }, function (error2) { 
       errorCallback(error); 
      }); 
      return; 
     } 
     errorCallback(error); 
    }); 
} 

function getFile(fileName, parentDir, callback, errorCallback) { 
    if (parentDir == null) { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
      function (fs) { 
       getFile(fileName, fs.root, callback, errorCallback); 
      }, 
      function (error) { 

      }); 
     return; 
    } 
    if (fileName.indexOf('/') >= 0) { 
     var dirName = fileName.substring(0, fileName.lastIndexOf('/')); 
     createDirectoryRecursively(dirName, parentDir, 
      function (directory) { 
       var name = fileName.substring(fileName.lastIndexOf('/') + 1); 
       getFile(name, directory, callback, errorCallback); 
      }, errorCallback); 
     return; 
    } 
    parentDir.getFile(fileName, { create: true }, 
     function (fileEntry) { 
      console.log('obtained file'); 
      callback(fileEntry); 
     }, 
     function (error) { 
      if (error.code == 1) { 
       parentDir.getFile(fileName, { create: false }, 
        function (fileEntry) { 
         console.log('obtained file'); 
         callback(fileEntry); 
        }, 
        function (error) { 
         if (error.code == 12) { 
          return; 
         } 
         errorCallback(error); 
        }); 
       return; 
      } 
      errorCallback(error); 
     }); 
} 

function readPersistantFile(filePath, successCallback, errorCallback) { 
    getFile(filePath, null, 
     function (fileEntry) { 
      console.log('obtained file'); 
      fileEntry.file(function (file) { 
       console.log('file size: ' + file.size); 
       var reader = new FileReader(); 

       reader.onloadend = function() { 
        console.log("Successful file read"); 
        if (successCallback != undefined) { 
         successCallback(this.result); 
        } 
       }; 

       var extension = filePath.substring(filePath.lastIndexOf('.') + 1); 
       if (extension == 'json') { 
        reader.readAsText(file); 
       } else { 
        reader.readAsDataURL(file); 
       } 
      }); 

     }, 
     function (error) { 
      console.log('An error occured while getting the file'); 
      console.error(error); 
      if (errorCallback != undefined) { 
       errorCallback(error); 
      } 
     }); 
} 

function writePersistantFile(filePath, data, isBinary, contentType, successCallback, errorCallback) { 
    var dataSize = (data.length != undefined) ? data.length : (data.size != undefined)? data.size : 5 * 1024 * 1024; 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, dataSize, function (fs) { 
     console.log('file system opened: ' + fs.name); 

     var lastIndex = filePath.lastIndexOf('/'); 
     var directory = filePath.substring(0, lastIndex); 
     createDirectoryRecursively(directory, fs.root, function (parentDir) { 
      console.log('Directory created'); 
      var fileName = filePath.substring(lastIndex + 1); 
      getFile(fileName, parentDir, 
       function (fileEntry) { 
        console.log('obtained file'); 
        fileEntry.createWriter(function (fileWriter) { 
         console.log('created writer'); 
         if (typeof (data) == 'string') { 
          fileWriter.write(new Blob([data], { type: contentType })); 
         } else { 
          fileWriter.write(data); 
         } 

         if (successCallback != undefined) { 
          successCallback(fileEntry); 
         } 
        }); 
       }, 
       function (error) { 
        console.log('An error occured while getting the file'); 
        console.error(error); 
        if (errorCallback != undefined) { 
         errorCallback(error); 
        } 
       }); 

     }, function (error) { 
      console.log('An error occured while saving the file'); 
      console.error(error); 
      if (errorCallback != undefined) { 
       errorCallback(error); 
      } 
     }); 
    }); 
} 

答えて

0

簡単に最適化することができ、上記のコード、のではなく、渡されるフォルダの長さ/数ではないと思われる。

書き込まれている同じファイルには、36文字の長さを有しており、 4つのサブフォルダ。これを 'page-1.jpg'のような単一のファイル名に変更することは問題なく働いた。

関連する問題