如何生成并提示从客户端浏览器中的内容保存文件? [英] How to generate and prompt to save a file from content in the client browser?

查看:103
本文介绍了如何生成并提示从客户端浏览器中的内容保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要给我的用户选择将存储在他们的客户端内存中的一些数据保存到磁盘。目前的解决方法是使用这样的处理程序

 (define-handler(download-deck)((deck:json) )
(setf(header-out:content-type)application / json
(header-out:content-disposition)附件)
deck)

它确实是什么样子。客户端向上发送数据,并将返回的文件保存在本地。



这似乎很愚蠢。



请,请告诉我有一个更好的,更简单,跨浏览器的方式让客户端使用文件保存对话框将一些本地数据保存到其磁盘。



我在这个主题上阅读的每一个答案都说不,你不能用javascript保存文件或是的,这是一个半记录的Chrome API可以让你在三个页面上执行。

解决方案

这可能有帮助。如果您希望它是合理的跨浏览器,那么您还需要在其所在的地方实施W3C Blob API还没有实现两者都是尊重命名空间,并且完全是框架无关的,所以不要担心命名问题。



一旦你有这些包含,只要你只有保存文本文件(我感谢您的关注),您应该能够

  var blob = new Blob([ Hello,world!],{type:text / plain; charset = utf-8}); 
saveAs(blob,hello world.txt);

请注意,新Blob的第一个参数必须是字符串列表,并且您希望指定文件名。与此同时,用户将看到此文件在本地下载,但不能自己命名。希望他们正在使用一个处理本地文件名冲突的浏览器...


I have a situation where I need to give my users the option to save some data stored locally in their client memory to disk. The current workaround I have is having a handler like this

(define-handler (download-deck) ((deck :json))
  (setf (header-out :content-type) "application/json"
    (header-out :content-disposition) "attachment")
  deck)

which does exactly what it looks like. The client sends their data up, and saves the returned file locally.

This seems stupid.

Please, please tell me there's a better, simpler, cross-browser way to let a client save some local data to their disk with a file-save dialog box.

Every answer I read on the subject either says "no, you can't save files with javascript" or "yes, there's this one semi-documented piece of the Chrome API that might let you do it in three pages".

解决方案

This may help. If you want it to be reasonably cross-browser, you'll also need this to implement the W3C Blob API in places it's not already implemented. Both respect namespaces, and are completely framework agnostic, so don't worry about naming issues.

Once you've got those included, and as long as you're only saving text files (I am, thank you for your concern), you should be able to

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

Note that the first argument to new Blob has to be a list of strings, and that you're expected to specify the filename. As in, the user will see this file being downloaded locally, but won't be able to name it themselves. Hopefully they're using a browser that handles local filename collisions...

这篇关于如何生成并提示从客户端浏览器中的内容保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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