2013年10月24日 星期四

javascript-write-and-download-file (chrome)


javascript


function download() {
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
fs.root.getFile('targetFile.txt', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
str=_lookup("SourceData");
var buf = new ArrayBuffer(str.length*2);
var arr = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
arr[i] = str.charCodeAt(i);
}
var blob = new Blob([arr]);
fileWriter.addEventListener("writeend", function() {
location.href = fileEntry.toURL();
}, false);
fileWriter.write(blob);
}, function() {});
}, function() {});
}, function() {});
}

<a href="#"><button class="SaveButton2" onclick="download();">Download and Open</button></a>

沒有留言:

張貼留言