Javascript将数据保存到文件系统(使用用户提示符) [英] Javascript save data to file system (with user prompt)

查看:102
本文介绍了Javascript将数据保存到文件系统(使用用户提示符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的最新浏览器中实现以下功能的最佳方式是什么(支持html5)。我主要针对谷歌浏览器。

在我的应用程序中,数据是通过JavaScript处理的,需要使用浏览器提示(另存为对话框)将输出写入文件系统。我不确定要写入文件系统的安全限制,但我不打算匿名写入(但会提示用户并选择位置)。



我看到 saveAs 尚未原生支持。在我的研究中,我看到很少的选择。


  1. https://github.com/eligrey/FileSaver.js

  2. a.download( http://html5-demos.appspot.com/static/a.download.html

  3. web文件系统

  4. 对象URL

FileServer重新回到2,3, 4如果没有本地支持,我可以使用它。但是,我找不到打开另存为对话框的方法。它只是将文件保存在默认位置(在mac中下载文件夹)。



您将使用哪种方式在最新的浏览器中获得良好的支持?
如何打开另存为对话框并让用户命名文件。



谢谢。

解决方案

我同意Nathan Hayfield。浏览器将为您处理保存对话框。设置标题Content-Disposition会将响应作为附件发回,浏览器将从那里处理它。
eg。

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


What is the best way to achieve the below in latest latest browsers (with html5 support). 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). 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. With my research, I see few options.

  1. https://github.com/eligrey/FileSaver.js
  2. a.download (http://html5-demos.appspot.com/static/a.download.html)
  3. web filesystem
  4. object URLs

FileServer falls back to 2,3,4 if no native support and I may use it. 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).

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. eg.

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

这篇关于Javascript将数据保存到文件系统(使用用户提示符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆