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

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

问题描述

我有一个棘手的问题要问你,我已经为此纠结了一段时间.

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

我正在寻找一种解决方案,我可以在其中将文件保存到用户计算机,而无需本地存储,因为本地存储有 5MB 的限制.我想要保存到文件"对话框,但我想要保存的数据仅在 javascript 中可用,我想防止将数据发送回服务器然后再次发送.

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.

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

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.

我找到了一个 javascript 函数,可以通过保存到文件"对话框将数据保存到用户计算机,但有关此工作的工作已停止且未得到完全支持.是这样的:http://www.w3.org/TR/file-writer-api/

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/

既然我没有 window.saveAs,那么有什么方法可以从 Blob 对象中保存数据而不将所有内容发送到服务器?

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.

我知道这行得通,因为 MEGA 正在这样做,但我想要我自己的解决方案 :)

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

推荐答案

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

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);

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

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天全站免登陆