javascript Mega 如何在不需要许可的情况下将文件保存到硬盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21019796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Mega does to save a file to harddrive without requiring permission
提问by Kiwy
I'm not sure if the place to ask this question so feel free to move my question elsewhere or closed it.
I have been told that Javascript can write and read from disk only when an explicit event like drag & drop happen on the browser and gave it a handle to a file.
我不确定问这个问题的地方是否可以随意将我的问题移到其他地方或关闭它。
有人告诉我,只有当浏览器上发生拖放等显式事件并为其提供文件句柄时,Javascript 才能从磁盘写入和读取。
In the case of Mega(ex upload) when you click download you are directly saving a file to disk without being asked to save it or installing any extension your browser.
Is it a feature of Javascript or HTML 5 because in my opinion it means a terrible security issue in this case.
在 Mega(ex 上传)的情况下,当您单击下载时,您将直接将文件保存到磁盘,而不会被要求保存或安装任何扩展程序您的浏览器。
它是 Javascript 还是 HTML 5 的一个特性,因为在我看来,在这种情况下它意味着一个可怕的安全问题。
Edit 1:So my question is, how do Mega manage to write on disk without showing you the Save to
dialog of your browser popped up and self determining where to put the file
编辑 1:所以我的问题是,Mega 如何在不向您显示Save to
弹出的浏览器对话框并自行确定文件放置位置的情况下设法写入磁盘
采纳答案by johnnycardy
It's just a matter of sending the right headers in the response - so the browser knows to save instead of open. It's nothing to do with javascript or HTML5.
这只是在响应中发送正确标头的问题 - 因此浏览器知道保存而不是打开。它与 javascript 或 HTML5 无关。
For example, in PHP:
例如,在 PHP 中:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
I don't know what server-side technology Mega uses but this is the basic premise.
我不知道 Mega 使用什么服务器端技术,但这是基本前提。
Edit:in response to your edited question about why the 'Save As' dialog does not appear, well, that's a browser setting. Chrome, for example, will by default save to your Downloads folder (on Windows) without prompting.
编辑:为了回答您编辑的关于为什么“另存为”对话框没有出现的问题,好吧,这是浏览器设置。例如,Chrome 会默认保存到您的下载文件夹(在 Windows 上)而不提示。
回答by brunoric
The question is somewhat vague, to answer properly maybe we need more information about your system and browser vendor/version. Mega uses some really cool stuff to handle downloads and files.
这个问题有点含糊,要正确回答,也许我们需要有关您的系统和浏览器供应商/版本的更多信息。Mega 使用一些非常酷的东西来处理下载和文件。
They use, depending of the client environment:
他们使用,具体取决于客户端环境:
- Filesystem API (Chrome / Firefox Extension polyfill);
- Adobe Flash SWF Filewriter (fallback for old browsers);
- BlobBuilder (IE10/IE11);
- MEGA Firefox Extension (deprecated);
- Arraybuffer/Blob Memory Based;
- MediaSource (experimental streaming solution);
- IndexedDB blob based (Firefox 20+).
- 文件系统 API(Chrome / Firefox 扩展 polyfill);
- Adobe Flash SWF Filewriter(旧浏览器的后备);
- BlobBuilder (IE10/IE11);
- MEGA Firefox 扩展(已弃用);
- 基于 Arraybuffer/Blob 内存;
- MediaSource(实验性流媒体解决方案);
- 基于 IndexedDB blob(Firefox 20+)。
With all these methodologies, download directly without user intervention/authorization will rely on the browser compatibility. I am using Firefox on Linux, when I choose a file from Mega and click to download, a dialog box shows up so, in matter of effect, I have to authorize the download. But if you look to this screnshot, in the 'from' field you will see the word 'blob', thats a sign of a Blob objectfrom the File API W3C Specification.
使用所有这些方法,无需用户干预/授权直接下载将依赖于浏览器兼容性。我在 Linux 上使用 Firefox,当我从 Mega 中选择一个文件并单击下载时,会出现一个对话框,因此,实际上,我必须授权下载。但是,如果你看看这个screnshot“从”字段,你会看到这个词“斑点”,在,那是一个的标志Blob对象从文件API W3C规范。
You can see a Blob API utilization example in this fiddleand inspecting Mega's source code(case 4: Arraybuffer/Blob Memory Based, lines 15, 293, 324, 802).
您可以在此小提琴中看到 Blob API 使用示例并检查Mega 的源代码(案例 4:基于 Arraybuffer/Blob Memory,第 15、293、324、802 行)。
window.URL = window.URL || window.webkitURL;
var blobExample = new Blob(['\
<!doctype html>\n\
<html>\n\
<body>Hello from Blob file!</body>\n\
</html>'], {type: 'text/html'});
var blobLink = document.createElement('link');
blobLink.rel = 'html';
blobLink.href = window.URL.createObjectURL(blobExample);
document.body.appendChild(blobLink);
var anchor = document.createElement('a');
anchor.href = window.URL.createObjectURL(blobExample);
anchor.download = 'blob-example.html';
anchor.textContent = 'Download the binary large object';
document.body.appendChild(anchor);
=)
=)
回答by Lubo? Stará?ek
Strange... I'm using Mega on daily basis and it always shows the Save to dialog. Doesn't this depends on your browser settings?
奇怪...我每天都在使用 Mega,它总是显示“保存到”对话框。这不取决于您的浏览器设置吗?
I'm not having enough reputation to add a comment, so I had to post this as an answer, sorry for that.
我没有足够的声誉来添加评论,所以我不得不将此作为答案发布,抱歉。