2014-03-27 82 views
0

我爲win8商店創建了應該能夠保存生成數據的javascript/html應用程序。 文本數據保存到我使用一個文件:在Windows 8應用程序商店中支持javascript'msSaveBlob'

 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

我用下面的方法將圖像保存在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) 
      }); 

}

+0

不錯!雖然有2個問題:如何保存txt數據以及如何將文件夾從picturesLibrary更改爲下載? – 31415926

+0

你應該有公司帳戶使用文檔庫,並且它有許多限制.u可以使用filepickers將數據保存在文檔庫中。引用此鏈接http://blogs.msdn.com/b/wsdevsol/archive/2013/05/ 09/deal-with-documents-how-to-use-the-documentslibrary-capability-in-windows-store-apps.aspx – suganthi

+0

保存文本請點擊此鏈接http://msdn.microsoft.com/en-我們/庫/窗/應用/ dn531045.aspx – suganthi

相關問題