Question

I'm creating javascript/html app for win8 store which should be able to save generated data. To save text data to a file I use:

        var bb = new MSBlobBuilder();
        bb.append(data);
        var blob = bb.getBlob("text/plain");
        window.navigator.msSaveBlob(blob, 'fname.txt');

To save image I use:

        var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        window.navigator.msSaveBlob(canvas.msToBlob(), 'fileName.png');

These parts of code work well in IE11.

But when I ran my code via Visual Studio I've got an error: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'msSaveBlob'

How to solve this problem?
Or is there any other alternatives to save data in win8 apps?

Was it helpful?

Solution

i have used the following method to save images in windows store apps

        var saveimg= function () { 
        var output;
        var input;
        var outputStream;
        var Imaging = Windows.Graphics.Imaging;
        var fileval = null;
        imageFile = null;
        Windows.Storage.KnownFolders.picturesLibrary.createFileAsync("filename.png",
               Windows.Storage.CreationCollisionOption.generateUniqueName).
            then(function (file) {
                fileval = file;
                return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
            }).then(function (stream) {                    
                fileStream = stream;                  
               var canvas = document.createElement('canvas');
               canvas.id     = "canvasid";
                canvas.width = img.width;
                canvas.height = img.height;
                var ctx = canvas.getContext('2d');
                ctx.drawImage(img, 0, 0, canvas.width, canvas.height);                                  
                return Imaging.BitmapEncoder.createAsync(Imaging.BitmapEncoder.pngEncoderId, stream);

                // return Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output);
            }).then(
            function (encoder) {
                //Set the pixel data--assume "encoding" object has options from elsewhere
                //  encoder.setPixelData(encoding.pixelFormat, encoding.alphaMode, encoding.width, encoding.height, encoding.dpiX, encoding.dpiY, new Uint8Array(imgData.data)); //Go do the encoding
                var canvas = document.getElementById("canvasid");
                var ctx = canvas.getContext("2d");                  
                var width = document.getElementById("canvasid").width;
                var height = document.getElementById("canvasid").height;                  
                var outputPixelData = ctx.getImageData(0, 0, width, height);
                encoder.setPixelData(
                    Imaging.BitmapPixelFormat.rgba8,
                    Imaging.BitmapAlphaMode.straight,
                    width,
                    height,
                    96, // Horizontal DPI
                    96, // Vertical DPI
                    outputPixelData.data
                    );
                return encoder.flushAsync();
            }).done(function () {

                fileStream.close();
            }, function () {
                //Empty error handler (do nothing if the user canceled the picker)
            });

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top