简体   繁体   English

Javascript将数据保存到文件系统(带有用户提示)

[英]Javascript save data to file system (with user prompt)

What is the best way to achieve the below in latest latest browsers (with html5 support). 在最新的最新浏览器中实现以下功能的最佳方式是什么(支持html5)。 I mainly target Google Chrome. 我主要针对谷歌浏览器。

In my application, data is manipulated through javascript and needs to write output to the file system with a browser prompt (save as dialog). 在我的应用程序中,数据是通过javascript操作的,需要使用浏览器提示将输出写入文件系统(另存为对话框)。 I am not sure about the security restrictions to write to file system, but I am not planning anonymous write (but user is prompted, and selects the location). 我不确定写入文件系统的安全限制,但我不打算匿名写入(但提示用户,并选择位置)。

I see saveAs not natively supported yet. 我看到saveAs还没有原生支持。 With my research, I see few options. 通过我的研究,我看到几个选项。

  1. https://github.com/eligrey/FileSaver.js https://github.com/eligrey/FileSaver.js
  2. a.download ( http://html5-demos.appspot.com/static/a.download.html ) a.download( http://html5-demos.appspot.com/static/a.download.html
  3. web filesystem 网络文件系统
  4. object URLs 对象URL

FileServer falls back to 2,3,4 if no native support and I may use it. 如果没有本机支持,FileServer可以回退到2,3,4,我也可以使用它。 But, I don't find a way to open a Save As dialog. 但是,我找不到打开“另存为”对话框的方法。 It just save a file in default location (downloads folder in mac). 它只是将文件保存在默认位置(在mac中下载文件夹)。

Which option would you use to get a good support in latest browsers? 您将使用哪个选项在最新浏览器中获得良好支持? How can get open the Save As dialog and let the user name the file. 如何打开“另存为”对话框并让用户为该文件命名。

Thanks. 谢谢。

I agree with Nathan Hayfield. 我赞同内森·海菲尔德。 The browser will handle the save dialog box for you. 浏览器将为您处理保存对话框。 Setting the header Content-Disposition will send the response back as an attachment and the browser will handle it from there. 设置标题Content-Disposition会将响应作为附件发回,浏览器将从那里处理它。 eg. 例如。

content.Headers.Add("Content-Disposition", "attachment; filename=export.csv");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM