从斑点
问题描述:
保存到本地文件我有一个棘手的问题给你,这我现在struggeling了一段时间。从斑点
我正在寻找一个解决方案,在那里我可以将文件保存到用户的计算机,没有本地存储,因为本地存储5MB有限制。我想“保存到文件” - 对话,但我想保存数据只适用于JavaScript的,我想,以防止发送数据到服务器,然后再发送。
用例是,即时处理的服务正在保存用户数据的压缩和加密块,因此服务器不知道这些块中的内容,并通过将数据发送回服务器,这会导致4倍流量,并且服务器正在接收未加密的数据,这将导致整个加密无效。
我发现了一个JavaScript函数数据到用户计算机保存“保存到文件” - 对话,但在这个工作已经停产,心不是完全支持。这是这样的:http://www.w3.org/TR/file-writer-api/
因此,因为我没有window.saveAs,有什么办法保存Blob对象的数据而不发送一切到服务器?
将是巨大的,如果我能得到一个提示,该怎么寻找。
我知道这个工程,因为MEGA是这样做的,但我想我自己的解决方案:)
答
你最好的选择是使用一个blob URL(这是一个特殊的URL指向的对象浏览器的内存):
var myBlob = ...;
var blobUrl = URL.createObjectURL(myBlob);
现在你可以选择简单地重定向到该网址(window.location.replace(blobUrl)
),或创建一个链接。第二个解决方案允许您指定一个默认的文件名:
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
答
从文件选择或输入类型=文件文件选择,文件名保存到本地存储:
HTML:
<audio id="player1">Your browser does not support the audio element</audio>
的JavaScript:
function picksinglefile() {
var fop = new Windows.Storage.Pickers.FileOpenPicker();
fop.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.musicLibrary;
fop.fileTypeFilter.replaceAll([".mp3", ".wav"]);
fop.pickSingleFileAsync().then(function (file) {
if (file) {
// save the file name to local storage
localStorage.setItem("alarmname$", file.name.toString());
} else {
alert("Operation Cancelled");
}
});
}
然后后面的代码,当你要玩你的Selec文件请使用以下命令,该命令仅使用音乐库中的名称来获取文件。 (在UWP包清单,将您的“Capabilites”包括“音乐库”。)在这里发布
var l = Windows.Storage.KnownFolders.musicLibrary;
var f = localStorage.getItem("alarmname$").toString(); // retrieve file by name
l.getFileAsync(f).then(function (file) {
// storagefile file is available, create URL from it
var s = window.URL.createObjectURL(file);
var x = document.getElementById("player1");
x.setAttribute("src", s);
x.play();
});
类似的问题 - http://*.com/questions/13405129/javascript-create-and-save -file这里 - http://*.com/questions/18690450/saving-a-file-with-javascript – 2014-08-28 11:59:20