【问题标题】:How to implement 'save as' button in web application如何在 Web 应用程序中实现“另存为”按钮
【发布时间】:2017-04-11 11:16:19
【问题描述】:

我必须在我的 vaadin-oracle 表单应用程序中实现“另存为”按钮,但我不知道该怎么做。我不能为此使用Applet。 数据将被保存并存储在硬盘上(以各种文件格式)。

我读到 javascript 对此不安全。 第二个想法是创建简单的网络服务,它可以托管在本地主机上,并从网络应用程序发送数据到那里保存。

有没有解决方法的教程或想法?

【问题讨论】:

    标签: javascript web-applications vaadin oracleforms


    【解决方案1】:

    在这里查看 Davide Rizzo 的 CODEPEN,它的 javascript 但非常方便!

    此代码使用 FileSaver.js

    $("#btn-save").click( function() {
      var text = $("#textarea").val();
      var filename = $("#input-fileName").val()
      var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
      saveAs(blob, filename+".txt");
    });
    body { 
      padding: 1em;
      background: #EEE;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14082/FileSaver.js"></script>
    <form role="form">
      <h3>Saving a file with pure JS!</h3>
      <p>Uses HTML5 W3C saveAs() function and the <a href="https://github.com/eligrey/FileSaver.js" target="_blank">FileSaver.js</a> polyfill for this.<br>
      I didn't think this was even possible without a server but the docs say it should work in IE 10+,  Sweet!</p>
      <div class="form-group">
        <label for="input-fileName">File name</label>
        <input type="text" class="form-control" id="input-fileName" value="textFile" placeholder="Enter file name">
      </div>
      <div class="form-group">
        <label for="textarea">Text</label>
        <textarea id="textarea" class="form-control" rows="10" placeholder="Enter text to save">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae iure ab voluptate sunt reiciendis, officia, aliquam temporibus. Quo laudantium quaerat ad, deleniti optio ex, dignissimos, ea accusamus placeat tempora minima!</textarea>
      </div>
      <button id="btn-save" type="submit" class="btn btn-primary">Save to file</button>
    </form>

    【讨论】:

    • 非常感谢,我会将您的解决方案应用于 xls 文件类型和其他文件类型。还有一个问题:我必须为 IE 9 实现它,你的解决方案是否也适用于这个版本,还是我必须找到另一个?
    • FileSaver.js 仅支持 IE 10+ see here for support - jQuery 支持 IE 9+ see here for support
    猜你喜欢
    • 2011-07-09
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多