在html中保存文件的方法?

时间:2014-11-27 05:17:45

标签: javascript html webserver centos7

所以我一直在搞乱HTML和Literally Canvas,这基本上只是为你创建了一个绘图板,并且得到了它,所以我的页面上有一个导出按钮。当您点击此按钮时,它目前只打开带有图像的新页面;我想要完成的是当有人点击按钮时,将文件保存在我的网络服务器本地。

打开图纸的行是:

      window.open(lc.getImage().toDataURL());

有没有办法实现这个目标?

这是我的完整代码。

<html>
  <head>
    <!-- stylesheet -->
    <link href="/draw/literallycanvas-0.4.1/css/literallycanvas.css" rel="stylesheet">

    <!-- dependency: React.js -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.10.0/react-with-addons.js"></script>

    <!-- Literally Canvas -->
    <script src="/draw/literallycanvas-0.4.1/js/literallycanvas.js"></script>
  </head>
  <body>
    <!-- where the widget goes. you can do CSS to it. -->


    <!-- kick it off -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></src>
    <script>
        // Look ma, no jQuery!
        LC.init(
            document.getElementsByClassName('literally')[0],
            {imageURLPrefix: '/draw/literallycanvas-0.4.1/lib/img'}
        );

        /* or if you just love jQuery,
            $('.literally').literallycanvas({imageURLPrefix: '/draw/literallycanvas-0.4.1/lib/img'})
        */


    </script>


<div class="literally export"></div>
<form class="controls export">
  <input type="submit" data-action="export-as-png" value="Export as PNG">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    var lc = LC.init(document.getElementsByClassName('literally export')[0]);
    $('.controls.export [data-action=export-as-png]').click(function(e) {
      e.preventDefault();
      window.open(lc.getImage().toDataURL());
    });
  });
</script>





  </body>
</html>

1 个答案:

答案 0 :(得分:1)

您将无法将图像保存到网络服务器,因为一切都在客户端。您最好的选择是在获取dataUrl后上传图像。此问题说明了如何执行此操作:How to save a HTML5 Canvas as Image on a server