Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Created November 17, 2012 04:06
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 lukehoban/4093199 to your computer and use it in GitHub Desktop.
Save lukehoban/4093199 to your computer and use it in GitHub Desktop.
Creating a Blob from a Windows IBuffer or RandomAccesStream
// If you are starting with a file from a picker, something like this should work:
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.fileTypeFilter.replaceAll(['.jpg', '.jpeg']);
picker.pickSingleFileAsync().then(function (file) {
return file.openAsync(Windows.Storage.FileAccessMode.read);
}).done(function (ras) {
var blob = MSApp.createBlobFromRandomAccessStream('image/jpeg', ras);
var img = document.createElement('img');
document.body.appendChild(img);
img.src = URL.createObjectURL(blob);
})
// If you are starting with a WinRT buffer, the below works, but there may be something more direct as well:
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.fileTypeFilter.replaceAll(['.jpg', '.jpeg']);
picker.pickSingleFileAsync().then(function (file) {
return Windows.Storage.FileIO.readBufferAsync(file);
}).done(function (buffer) {
var reader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
var bytes = new Uint8Array(buffer.length);
reader.readBytes(bytes);
var blob = new Blob([bytes], { type: 'image/jpeg' });
var img = document.createElement('img');
document.body.appendChild(img);
img.src = URL.createObjectURL(blob);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment