2014-02-24 71 views
0

我使用webworker和同步方法在文件系統中創建文件。在Chrome中使用fileWriterSync寫入文件

一切正常,但當chrome更新到33版本時只能寫入一個文件。第二個文件顯示此錯誤:

ERROR: InvalidStateError: Failed to execute 'write' on 'FileWriterSync': An operation that depends on state cached in an interface object was made but the state had changed since it was read from disk.

Android中鉻(與版本32)都可以與桌面版Chrome的版本32是一切OK了。

是一個錯誤?

這個代碼是在bucle(同步文件系統!!)

fs.root.getDirectory('/'+dir+'/'+dir_image, {create: true}); 

fileEntry=fs.root.getFile('/'+dir+'/'+dir_image+'/f_'+filename, {create: true}); 

blob = new Blob([xhr2BLOB.response], {type: 'image/'+arr_image[2]}); 

writer=fileEntry.createWriter(); 

writer.write(blob); //here ERROR second time!!! only version 33!!! 
+0

解決方案:將xh2的響應類型更改爲'blob'a'arraybuffer'並創建如下所示的blob:'code'var arrayBuffer = xhr2BLOB.response; var blob = new Blob([new Uint8Array(arrayBuffer)],{type:'image /'+ arr_image [2]});'code' – user3346250

回答

0

解決方案:

XHR2的

變化的responseType(XMLHttpRequest的)從 '斑點' 到 'arraybuffer' 和創建BLOB這樣:

var xhr2BLOB = new XMLHttpRequest(); 
xhr2BLOB.responseType='arraybuffer'; 

xhr2BLOB.open('GET',path_to_image, false); //SYNCHRONOUS 
xhr2BLOB.send(); 

var arrayBuffer = xhr2BLOB.response; 

fs.root.getDirectory('/'+dir, {create:true}); 

fs.root.getDirectory('/'+dir+'/'+dir_image, {create: true}); 
fileEntry=fs.root.getFile('/'+dir+'/'+dir_image+'/f_'+filename, {create: true}); 


var blob = new Blob([new Uint8Array(arrayBuffer)],{type:'image/'+arr_image[2]}); 

fileEntry.createWriter().write(blob); 

記住,用同步的XMLHttpRequest和同步文件系統,這是一個webworker代碼。