Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created September 9, 2013 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/6496968 to your computer and use it in GitHub Desktop.
Save tateisu/6496968 to your computer and use it in GitHub Desktop.
function formatFileError(err){
var sb = err.toString();
for( var k in err ){
sb += "\n "+k+"="+err[k];
if( k == "code" ){
sb+=" ";
switch (err[k]) {
case FileError.QUOTA_EXCEEDED_ERR: sb += 'QUOTA_EXCEEDED_ERR'; break;
case FileError.NOT_FOUND_ERR: sb += 'NOT_FOUND_ERR'; break;
case FileError.SECURITY_ERR: sb += 'SECURITY_ERR'; break;
case FileError.INVALID_MODIFICATION_ERR: sb += 'INVALID_MODIFICATION_ERR'; break;
case FileError.INVALID_STATE_ERR: sb += 'INVALID_STATE_ERR'; break;
};
}
}
return sb;
}
function testFileSave(){
try{
var save_file = "file1";
var save_data = JSON.stringify({ key1: 'v1',key2: 'v2',key3: 'v3'});
// PERSISTENT or TEMPORARY
window.storageInfo.requestQuota(window.PERSISTENT, 8 * 1024 * 1024, function(granted_bytes){
try{
if(! window.requestFileSystem ){
window.requestFileSystem = window.webkitRequestFileSystem;
}
window.requestFileSystem( window.PERSISTENT, granted_bytes, function(fs){
try{
fs.root.getFile( save_file, {create: true }, function(entry){
// entry.isFile // entry.name // entry.fullPath
try{
entry.createWriter(function(writer) {
try{
writer.onwriteend = function(e) {
console.log('Write completed.');
};
writer.onerror = function(e) {
console.log("write error:"+ formatFileError(err) );
};
var bb = new BlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12.
bb.append(save_data);
writer.write(bb.getBlob('text/plain'));
}catch(exc){
console.log("write exception:"+formatFileError(exc));
}
},function(err){
console.log("createWriter failed."+formatFileError(err));
});
}catch(exc){
console.log("createWriter exception:"+formatFileError(exc));
}
},function(err){
console.log("getFile failed."+formatFileError(err));
});
}catch(exc){
console.log("getFile exception:"+formatFileError(exc));
}
},function(err){
console.log("requestFileSystem failed."+formatFileError(err));
});
}catch( exc ){
console.log("requestFileSystem exception:"+formatFileError(exc));
}
},function(err){
console.log("requestQuota failed."+formatFileError(err));
});
}catch(exc){
console.log("requestQuota exception:"+formatFileError(exc));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment