从Blob保存到本地文件 [英] Save to Local File from Blob

查看:198
本文介绍了从Blob保存到本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个困难的问题,我现在在努力一段时间。



我正在寻找一个解决方案,在那里我可以保存文件到用户计算机,没有本地存储,因为本地存储具有5MB的限制。我想要保存到文件-dialog,但我要保存的数据只能在javascript中使用,我想防止将数据发送回服务器,然后再次发送。



用例是,服务器正在保存用户数据的压缩和加密块,因此服务器不知道这些块中的内容和将数据发送回服务器,这将导致4次流量,服务器正在接收未加密的数据,这将使整个加密无用。



我发现一个javascript使用保存到文件-dialog将数据保存到用户计算机的功能,但是对此的工作已经停止,并且完全不支持。就这样: http://www.w3.org/TR/file-writer-api /



所以,因为我没有window.saveAs,从Blob对象保存数据的方法是什么,而不是将所有内容发送到服务器? p>

如果我能得到一个提示,要搜索的内容将会很棒。



我知道这是有效的,因为MEGA正在做,但我想要我自己的解决方案:)

解决方案

你最好的选择是使用一个blob网址一个指向浏览器内存中的对象的特殊URL):

  var myBlob = ...; 
var blobUrl = URL.createObjectURL(myBlob);

现在您可以选择简单地重定向到此url( window.location .replace(blobUrl)),或者创建一个链接。第二个解决方案允许您指定一个默认文件名:

  var link = document.createElement(a); //或者从当前文档获取
link.href = blobUrl;
link.download =aDefaultFileName.txt;
link.innerHTML =点击这里下载文件;
document.body.appendChild(link); //或附加它,无论你想要


I have a difficult question to you, which i'm struggeling on for some time now.

I'm looking for a solution, where i can save a file to the users computer, without the local storage, because local storage has 5MB limit. I want the "Save to file"-dialog, but the data i want to save is only available in javascript and i would like to prevent sending the data back to the server and then send it again.

The use-case is, that the service im working on is saving compressed and encrypted chunks of the users data, so the server has no knowledge whats in those chunks and by sending the data back to the server, this would cause 4 times traffic and the server is receiving the unencrypted data, which would render the whole encryption useless.

I found a javascript function to save the data to the users computer with the "Save to file"-dialog, but the work on this has been discontinued and isnt fully supported. It's this: http://www.w3.org/TR/file-writer-api/

So since i have no window.saveAs, what is the way to save data from a Blob-object without sending everything to the server?

Would be great if i could get a hint, what to search for.

I know that this works, because MEGA is doing it, but i want my own solution :)

解决方案

Your best option is to use a blob url (which is a special url that points to an object in the browser's memory) :

var myBlob = ...;
var blobUrl = URL.createObjectURL(myBlob);

Now you have the choice to simply redirect to this url (window.location.replace(blobUrl)), or to create a link to it. The second solution allows you to specify a default file name :

var link = document.createElement("a"); // Or maybe get it from the current document
link.href = blobUrl;
link.download = "aDefaultFileName.txt";
link.innerHTML = "Click here to download the file";
document.body.appendChild(link); // Or append it whereever you want

这篇关于从Blob保存到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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