<legend id='GCaan'><style id='GCaan'><dir id='GCaan'><q id='GCaan'></q></dir></style></legend>

          <bdo id='GCaan'></bdo><ul id='GCaan'></ul>
        <i id='GCaan'><tr id='GCaan'><dt id='GCaan'><q id='GCaan'><span id='GCaan'><b id='GCaan'><form id='GCaan'><ins id='GCaan'></ins><ul id='GCaan'></ul><sub id='GCaan'></sub></form><legend id='GCaan'></legend><bdo id='GCaan'><pre id='GCaan'><center id='GCaan'></center></pre></bdo></b><th id='GCaan'></th></span></q></dt></tr></i><div id='GCaan'><tfoot id='GCaan'></tfoot><dl id='GCaan'><fieldset id='GCaan'></fieldset></dl></div>
      1. <tfoot id='GCaan'></tfoot>

        <small id='GCaan'></small><noframes id='GCaan'>

      2. 尝试使用 HTML5 文件系统将画布 PNG 数据 url 保存到磁盘,但是当我作为 URL 检索时,它无效

        Trying to save canvas PNG data url to disk with HTML5 filesystem, but when I retrieve as URL, it#39;s invalid(尝试使用 HTML5 文件系统将画布 PNG 数据 url 保存到磁盘,但是当我作为 URL 检索时,它无效)
        <legend id='iadlP'><style id='iadlP'><dir id='iadlP'><q id='iadlP'></q></dir></style></legend>
            <tbody id='iadlP'></tbody>
            <tfoot id='iadlP'></tfoot>

                <bdo id='iadlP'></bdo><ul id='iadlP'></ul>

                <small id='iadlP'></small><noframes id='iadlP'>

              • <i id='iadlP'><tr id='iadlP'><dt id='iadlP'><q id='iadlP'><span id='iadlP'><b id='iadlP'><form id='iadlP'><ins id='iadlP'></ins><ul id='iadlP'></ul><sub id='iadlP'></sub></form><legend id='iadlP'></legend><bdo id='iadlP'><pre id='iadlP'><center id='iadlP'></center></pre></bdo></b><th id='iadlP'></th></span></q></dt></tr></i><div id='iadlP'><tfoot id='iadlP'></tfoot><dl id='iadlP'><fieldset id='iadlP'></fieldset></dl></div>

                  本文介绍了尝试使用 HTML5 文件系统将画布 PNG 数据 url 保存到磁盘,但是当我作为 URL 检索时,它无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我从画布中得到 base64 编码的图像:

                  I get the base64-encoded image form the canvas as:

                  var dataURL = canvas.toDataURL( "image/png" );
                  

                  然后我把它变成这样的数据:

                  Then I turn it into data like this:

                  //Remove the beginning identifier and use Chrome/Firefox?safari built int base64Decoder
                  var data = atob( dataURL.substring( "data:image/png;base64,".length ) );
                  

                  然后我通过以下方式将其写入文件系统:

                  Then I write it to the filesystem via:

                  event.createWriter(
                      function(writerEvent)
                      {
                          //The success handler
                          writerEvent.onwriteend = function(finishEvent)
                          {
                              ...
                          };
                  
                          //Error handler
                          writerEvent.onerror = settings.error;
                  
                          // Create a new Blob
                          var blob = new Blob( [ data ], { type: "image/png" } );
                  
                          //Write it into the path
                          writerEvent.write( blob );
                      }
                  }
                  

                  我尝试将其设置为这样的图像的 src:

                  I try to set it as src of an image like this:

                  document.getElementById( "saved" ).src = event.toURL();
                  

                  写入文件,我可以找到它并获得一个 url(通过读取它并使用事件:event.toURL().但图像显示为一个损坏的图像图标网页.我做错了什么?

                  That writes the file and I am able to find it and get a url (by reading it and using the event: event.toURL(). But the image shows as a broken image icon on the web page. What am I doing wrong?

                  推荐答案

                  data 是一个字符串,所以当你将它传递给 blob 时,二进制数据将是 UTF-8 编码的那个字符串.你要二进制数据不是字符串.

                  data is a string, so when you pass it to blob, the binary data will be that string in UTF-8 encoding. You want binary data not a string.

                  你可以这样做:

                  var canvas = document.createElement("canvas");
                  
                  
                  var dataURL = canvas.toDataURL( "image/png" );
                  var data = atob( dataURL.substring( "data:image/png;base64,".length ) ),
                      asArray = new Uint8Array(data.length);
                  
                  for( var i = 0, len = data.length; i < len; ++i ) {
                      asArray[i] = data.charCodeAt(i);    
                  }
                  
                  var blob = new Blob( [ asArray.buffer ], {type: "image/png"} );
                  

                  还有 canvas.toBlob 将来可用,但目前不在 Chrome 中.

                  There is also canvas.toBlob available in future but not currently in Chrome.

                  演示 http://jsfiddle.net/GaLRS/

                  这篇关于尝试使用 HTML5 文件系统将画布 PNG 数据 url 保存到磁盘,但是当我作为 URL 检索时,它无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  1. <tfoot id='wo4WT'></tfoot>

                      1. <legend id='wo4WT'><style id='wo4WT'><dir id='wo4WT'><q id='wo4WT'></q></dir></style></legend>
                          <tbody id='wo4WT'></tbody>
                      2. <small id='wo4WT'></small><noframes id='wo4WT'>

                          <bdo id='wo4WT'></bdo><ul id='wo4WT'></ul>
                        • <i id='wo4WT'><tr id='wo4WT'><dt id='wo4WT'><q id='wo4WT'><span id='wo4WT'><b id='wo4WT'><form id='wo4WT'><ins id='wo4WT'></ins><ul id='wo4WT'></ul><sub id='wo4WT'></sub></form><legend id='wo4WT'></legend><bdo id='wo4WT'><pre id='wo4WT'><center id='wo4WT'></center></pre></bdo></b><th id='wo4WT'></th></span></q></dt></tr></i><div id='wo4WT'><tfoot id='wo4WT'></tfoot><dl id='wo4WT'><fieldset id='wo4WT'></fieldset></dl></div>