在cordova

时间:2018-02-11 06:45:17

标签: android cordova cordova-plugin-file

我正在关注Using PhoneGap FileWriter.write for "big" files方法在cordova android应用程序中编写大文件。但在此我不能追加net mb数据。结果只是第一个1mb的数据。有人可以帮我解决这个问题吗?

我的代码

   function writeFile1(fileEntry, dataObj, isAppend, content1) {

   fileEntry.createWriter(function(fileWriter) {


       function writeFinish() {

           alert("The file has written successfully.", "success");

       }



       fileWriter.onerror = function(e) {

           messagefunctioning("Failed file write: " + e.toString(), "error");
       };


       if (!dataObj) {
           dataObj = new Blob(['No db query has been retrieved. Please try again later.'], {
               type: 'text/plain'
           });
       }


       data = dataObj;

       var written = 0;
       var BLOCK_SIZE = 1 * 1024 * 1024; // write 1M every time of write
       var bytelngth = getByteLen(data);

       function writeNext(cbFinish) {
           alert("endssss");


           var sz = Math.min(BLOCK_SIZE, bytelngth - written); //alert("sz"+sz);
           //sz=5;
           var sub = data.slice(written, written + sz); //alert("sub"+sub);

           if (isAppend) { //alert("append");
               try { //alert("append try");alert(fileWriter.length);
                   fileWriter.seek(fileWriter.length);

               } catch (e) {
                   alert("file doesn't exist!");
               }
           }
           fileWriter.write(sub);

           written += sz;
           fileWriter.onwrite = function(evt) {
               if (written < bytelngth) {
                   writeNext(cbFinish);
               } else
                   cbFinish();
           };

       }
       //alert("4");
       writeNext(writeFinish);



       // fileWriter.write(dataObj);
   });

}

0 个答案:

没有答案