将数据URI格式的文件从浏览器拖到桌面 [英] Drag File in Data URI format from Browser to Desktop
问题描述
此博文点out,有一种方法可以通过从浏览器拖放到桌面来下载文件。
As this blog post points out, there is a way to download files via drag and drop from browser to desktop.
我想以数据uri格式拖动文件(例如data:application / octet-stream; base64,eNcoDEdFiLEStuFf)到桌面。由于安全原因,我无法提供服务器下载的完整URL(文件需要在客户端处理)。
I want to drag a file in data uri format (e.g. "data:application/octet-stream;base64,eNcoDEdFiLEStuFf") to the desktop. I cannot provide a full URL to a server download due to security reasons (file needs to be handled clientside).
当我尝试在博客文章示例中给出的内容时,创建了一个内容和名称是当前时间戳的文件:
When I try what's given in the example of the blog post, a file which content and name is the current timestamp is created:
item.addEventListener("dragstart", function(evt) {
evt.dataTransfer.setData("DownloadURL", "data:application/octet-stream;base64,eNcoDEdFiLEStuFf");
}
我已经尝试更改格式参数,稍微调整数据格式并预先解除,但没有任何作用,我从来没有把任何数据放到我的桌面上。是否有任何方式来实现我正在寻找的东西,至少在某些浏览器中?
I already tried changing the format parameter, tweaking the format of the data a little and deconding beforehand but nothing works, I never get any of my data onto my desktop. Is there any way to accomplish what I am looking for, at least in some browsers?
顺便说一句,我们不使用jQuery。结果,如果有一个jQuery的解决方案,但这很可能不适用于我们的项目。
By the way, we do not use jQuery. As a result, it might be interesting if there is a solution with jQuery but this will most probably not be applicable for our project.
推荐答案
据我了解wnload URL应具有以下格式:
mime-type:file_name:URL。 URL是您的数据URI。
对于你的情况:
As far i understood download URL should have following format: mime-type:file_name:URL. Were URL is your data URI. For your case:
item.addEventListener("dragstart", function(evt) {
evt.dataTransfer.setData("DownloadURL", "application/octet-stream:fileName.bin:data:application/octet-stream;base64,eNcoDEdFiLEStuFf");
}
哪个应该创建fileName.bin文件。
Which should create fileName.bin file.
看看 http://jsfiddle.net/Andrei_Yanovich/jqym7wdh/
但看起来它只适用于chrome
Take a look at http://jsfiddle.net/Andrei_Yanovich/jqym7wdh/ But it looks like it works only in chrome
这篇关于将数据URI格式的文件从浏览器拖到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!