2013-08-01 65 views
1

phonegap上的FileWriter出現奇怪的問題(2.8.1)。每當我嘗試使用文件寫入器編寫文件時,都會創建一個文件,但文件中的唯一內容是一行表示{「type」:「application/pdf」,「size」:235577}的行。 與任何文件相同 - 類型和大小不同,但這是寫入文件的唯一內容。Phonegap filewriter只寫blob元數據...?

我使用Chrome版本28運行相同的代碼),它工作得很好,但使用Blob而不是WebKitBlobBuilder。所以我下載了一個使用WebKitBlobBuilder的老版本Chromium(版本20),並且工作正常。

爲了檢查我是否做了些奇怪的事情,我在使用FileReader轉儲文件的內容之前嘗試寫入它(使用readAsDataURL) - 內容看起來很好。

那麼...?

代碼如下:

function convert2blob(bin, type){ 
    // takes a BINARY or a BLOB input and returns a blob 
    var blob; 
    if (bin.size? true: false) {  
     console.log("its a blob already"); // do nothing??  Line 3494 
     blob = bin; 
    } else { 
     var arr = new Uint8Array(bin.length); 
     for (var i = 0; i < length; i++) { 
      arr[i] = bin.charCodeAt(i) & 0xff; 
     } 
     var bl = new window.BlobBuilder(); 
     bl.append(arr.buffer); 
     blob = bl.getBlob(type); 
    } 
    return blob; 
} 


    blob = convert2blob(resx, attinfo.contenttype); 
    console.log(blob.size);        // Line 3543 
    // just to verify the contents of the blob 
    // tested with fileReader - contents are valid 
    /* // just to check the file contents 
    var rdr = new FileReader(); 
     rdr.onloadend = function(evt){ 
      console.log(evt.target.result.slice(0,128)); 
     }; 
    rdr.readAsDataURL(blob); 
    */ 
    // its a blob - that's what we decided 
    global.tmpFilesystem.root.getFile(attinfo.name, { create: true }, function (fileEntry) { 
     console.log(fileEntry); 
     fileEntry.createWriter(function (fileWriter) { 
      console.log(fileWriter); 
      fileWriter.onwriteend = function (e) { 
       console.log(e.error);     // Line 3582 
       console.log("finished writing");  // Line 3583 
       console.log(e.target.length);   // Line 3584 
       runpopup(selectorid, fileEntry.toURL()); 
      }; 
      fileWriter.onerror = function(e) { 
       console.log(e); 
      }; 
      fileWriter.write(blob); 
     }, function(e) { 
      console.log(e); 
     }); 
    }, function(e) { 
     console.log(JSON.stringify(e)); 
    }); 

logcat的輸出如下:

I/Web Console(19696): {"encoding":null,"name":"005.jpg","fullPath":"005.jpg","contenttype":"image/jpeg"}:3521 
I/Web Console(19696): /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEB:5236 
I/Web Console(19696): 516857:5240 
I/Web Console(19696): using WebKitBlobBuilder:5246 
I/Web Console(19696): 516857:5250 
// above few lines are after fetching from Websql DB 

I/Web Console(19696): [object Blob]:3541 
I/Web Console(19696): its a blob already:3494 
I/Web Console(19696): 516857:3543 
I/Web Console(19696): [object Object]:3560 
I/Web Console(19696): [object Object]:3580 
I/Web Console(19696): undefined:3582 
I/Web Console(19696): finished writing:3583 
I/Web Console(19696): 36:3584 

與瀏覽器輸出更新============ 可比產量從20鉻:

{"encoding":null,"fullPath":"closed-padlock.png","name":"closed-padlock.png","doc_id":"FE261266-B04C-48F3-8557-7FCF160E7F9F","contenttype":"image/png"} - :3521 
iVBORw0KGgoAAAANSUhEUgAAAS4AAAHXCAYAAAAC3beSAAAABmJLR0QA/wD/AP+gvaeTAAAXWElEQVR4nO3debDVdf3H8de9XJZAsACX1CwNwWVSGhfck2LMTEzDpazB - :5236 
6051 - :5240 
using WebKitBlobBuilder - :5246 
6051 - :5250 
Blob - :3541 
its a blob already - :3494 
6051 - :3543 
FileEntry - :3560 
FileWriter - :3580 
undefined - :3582 
finished writing - :3583 
6051 - :3584 
filesystem:file:///temporary/closed-padlock.png 

回答

2

所以,代碼是相同的 - 唯一的區別是我從2.8.1升級到3.0.0今天下午(通過,2.7,2.8和2.9--當試圖隔離問題時)和FileWriter問題已經消失。文件正在正確寫入。 Simon's guide在獲得插件轉換方面有很大的幫助 - 我仍然有一個插件問題,但這不是一個大問題 - 應該能夠很快完成排序(我認爲)。事實證明,這畢竟不是一個插件問題,一切都很好。

順便說一句 - 對於那些誰不知道,它相當有啓發通過cordova.js文件看 - 發現了一個base64編碼解碼器已被納入那裏 - 希望與CouchDB的編解碼器兼容...