支持javascript' msSaveBlob'在Windows 8应用商店中

时间:2014-03-27 10:45:20

标签: javascript windows-8 windows-store-apps blob

我正在为win8商店创建javascript / html应用程序,它应该能够保存生成的数据。 要将文本数据保存到文件,我使用:

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

要保存图像我使用:

        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');

这些代码部分在IE11中运行良好。

但是当我通过Visual Studio运行我的代码时,我遇到了一个错误:0x800a01b6 - JavaScript运行时错误:对象不支持属性或方法'msSaveBlob'

如何解决这个问题?
或者还有其他方法可以在win8应用程序中保存数据吗?

1 个答案:

答案 0 :(得分:1)

我使用以下方法在Windows应用商店应用中保存图像

        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)
            });

}