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

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

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

        • <bdo id='widGO'></bdo><ul id='widGO'></ul>

      1. 从 HTML5 Canvas (readAsBinaryString) 获取二进制 (base64) 数据

        时间:2023-06-22
          <bdo id='tHSwn'></bdo><ul id='tHSwn'></ul>
          <i id='tHSwn'><tr id='tHSwn'><dt id='tHSwn'><q id='tHSwn'><span id='tHSwn'><b id='tHSwn'><form id='tHSwn'><ins id='tHSwn'></ins><ul id='tHSwn'></ul><sub id='tHSwn'></sub></form><legend id='tHSwn'></legend><bdo id='tHSwn'><pre id='tHSwn'><center id='tHSwn'></center></pre></bdo></b><th id='tHSwn'></th></span></q></dt></tr></i><div id='tHSwn'><tfoot id='tHSwn'></tfoot><dl id='tHSwn'><fieldset id='tHSwn'></fieldset></dl></div>

            • <small id='tHSwn'></small><noframes id='tHSwn'>

                <tbody id='tHSwn'></tbody>

            • <legend id='tHSwn'><style id='tHSwn'><dir id='tHSwn'><q id='tHSwn'></q></dir></style></legend>
              <tfoot id='tHSwn'></tfoot>

                  本文介绍了从 HTML5 Canvas (readAsBinaryString) 获取二进制 (base64) 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法将 HTML Canvas 的内容读取为二进制数据?

                  Is there any way of reading the contents of a HTML Canvas as binary data?

                  目前我有以下 HTML 来显示输入文件及其下方的画布:

                  At the moment I've got the following HTML to show an input file and the canvas below it:

                  <p><button id="myButton" type="button">Get Image Content</button></p>
                  <p>Input:<input id="fileInput" type="file"/></p>
                  <p>Canvas<canvas id="myCanvas" width="578" height="200"/></p>
                  

                  然后我设置了我的输入文件以正确设置画布,这可以正常工作:

                  I've then setup my input file to set the canvas correctly which works fine:

                  $('#fileInput').on('change', function() {
                      $.each(this.files, function() {
                          var image = new Image();
                              image.src = window.URL.createObjectURL(this);
                  
                          image.onload = function() {
                              $("canvas").drawImage({
                                  source: image,
                                  x: 50, y: 50,
                                  width: 100,
                                  fromCenter: false
                              });
                          };
                      });
                  });
                  

                  我现在需要在单击按钮时从画布中获取二进制数据(Base64 编码),以便将数据推送到服务器...

                  I now need to get the binary data (Base64 encoded) from the Canvas when the button is clicked so it'll push the data to the server...

                  最终结果是我需要为用户提供选择文件,裁剪/调整大小,然后单击按钮,此时编辑后的图像将上传到服务器(我做不到由于服务器端限制,服务器端裁剪/调整大小...)

                  The end result is that I need to provide the user with the ability to select a file, crop/resize it, and then click a button at which point the edited image will be uploaded to the server (I can't do server-side cropping/resizing due to server-side limitations...)

                  任何帮助都会很棒!干杯

                  Any help would be great! Cheers

                  推荐答案

                  canvas 元素提供了一个 toDataURL 方法,该方法返回一个 data:包含给定格式的 base64 编码图像数据的 URL.例如:

                  The canvas element provides a toDataURL method which returns a data: URL that includes the base64-encoded image data in a given format. For example:

                  var jpegUrl = canvas.toDataURL("image/jpeg");
                  var pngUrl = canvas.toDataURL(); // PNG is the default
                  

                  虽然返回值不是只是 base64 编码的二进制数据,但只要去掉方案和文件类型就可以得到你想要的数据是一件简单的事情.

                  Although the return value is not just the base64 encoded binary data, it's a simple matter to trim off the scheme and the file type to get just the data you want.

                  如果浏览器认为您已将任何从不同来源加载的数据绘制到画布上,则 toDataURL 方法将失败,因此此方法仅在您的图像文件从与其脚本执行此操作的 HTML 页面相同的服务器.

                  The toDataURL method will fail if the browser thinks you've drawn to the canvas any data that was loaded from a different origin, so this approach will only work if your image files are loaded from the same server as the HTML page whose script is performing this operation.

                  有关详细信息,请参阅 canvas API 上的 MDN 文档,其中包括关于 toDataURL 的详细信息,以及 关于 data: 的维基百科文章URI 方案,其中包含有关您将从该调用中收到的 URI 格式的详细信息.

                  For more information see the MDN docs on the canvas API, which includes details on toDataURL, and the Wikipedia article on the data: URI scheme, which includes details on the format of the URI you'll receive from this call.

                  这篇关于从 HTML5 Canvas (readAsBinaryString) 获取二进制 (base64) 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:HTML5 Canvas toDataURL 返回空白 下一篇:PHP+JS:如何将 HTML 表单中的文件上传为 Content-Type Multipart(通过 JS)?

                  相关文章

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

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

                      <tfoot id='ZaydX'></tfoot>
                      <legend id='ZaydX'><style id='ZaydX'><dir id='ZaydX'><q id='ZaydX'></q></dir></style></legend>