-
-
Save javiervidal/3893470 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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