Skip to content

Instantly share code, notes, and snippets.

@eamodeorubio
Created September 8, 2012 17:40
Show Gist options
  • Save eamodeorubio/3677775 to your computer and use it in GitHub Desktop.
Save eamodeorubio/3677775 to your computer and use it in GitHub Desktop.
Quick hack to save the contents of a canvas to a file in Windows 8 with JS
function saveToFile() {
var canvasData = document.getElementById('sampleCanvas').msToBlob();
var windowsStorage = Windows.Storage;
var inputStream, outputStream;
windowsStorage.KnownFolders.picturesLibrary.createFileAsync('sample.png', windowsStorage.CreationCollisionOption.replaceExisting)
.then(function (file) {
return file.openAsync(windowsStorage.FileAccessMode.readWrite);
}).then(function (out) {
inputStream = canvasData.msDetachStream();
canvasData.msClose();
outputStream = out;
return windowsStorage.Streams.RandomAccessStream.copyAsync(inputStream, out);
}).then(function () {
return outputStream.flushAsync();
}).done(function () {
inputStream.close();
outputStream.close();
}, function(err) {
console.log(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment