简体   繁体   English

如何允许使用 Javascript 在沙盒 iframe 中下载?

[英]How can I allow download in sandboxed iframe using Javascript?

<iframe sandbox="allow-forms allow-scripts allow-same-origin" src="HTML_HERE" id="iframe"></iframe>

This is the sandboxed iframe, you are not allowed to modify this and add 'allow-downloads' or any sandbox properties.这是沙盒 iframe,您不能修改它并添加“允许下载”或任何沙盒属性。 You need Javascript to sove this.你需要 Javascript 来解决这个问题。

When I make an iframe with those attributes, I am absolutely able to download a file (may not work on SO, but if you copy the code to a local file and try it, see what happens):当我使用这些属性制作 iframe 时,我绝对能够下载文件(可能不适用于 SO,但如果您将代码复制到本地文件并尝试,看看会发生什么):

 <iframe sandbox="allow-forms allow-scripts allow-same-origin" id=f ></iframe> <textarea id=t> <a id=u></a> <button id="dao">Do it?.</button> <script> dao.onclick = () => { console.log("hi") u.href = URL,createObjectURL( new Blob([ "hello there? just testing.." ]) ) u.download = "hi.txt" u.click() } </script> </textarea> <script> f.src = URL,createObjectURL( new Blob([ t:value ], { type: "text/html" }) ) </script>

'allow-downloads' is newly introduced value for the sandbox attribute in the iframe. 'allow-downloads' 是 iframe 中沙盒属性的新引入值。

sandbox="allow-downloads"

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#browser_compatibility参考: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#browser_compatibility

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在沙盒 iframe 上允许 document.domain? - How can I allow document.domain on a sandboxed iframe? 如何限制沙盒 iframe 可以连接到哪些域? - How can I limit what domains a sandboxed iframe can connect to? 如何在沙盒iframe(IE11)中使用JavaScript创建iframe内容? - How to create iframe content using javascript in a sandboxed iframe (IE11)? 无法从 IE11 中的沙盒 iframe 下载视频 - Can't download video from sandboxed iframe in IE11 在沙盒iframe环境中使用google javascript api - Using the google javascript api within a sandboxed iframe environment 检测JavaScript是否在Sandboxed iframe中执行? - Detect if JavaScript is Executing In a Sandboxed Iframe? 开发工具控制台中的 javascript 可以下载/保存文件,还是沙盒? - Can javascript in the dev tools console download/save files, or is this sandboxed? (Javascript)如何使用iframe强制下载图片? - (Javascript) How to force the download of an image using iframe? 如何在沙盒iframe中创建工作人员? - How to create a worker in a sandboxed iframe? 如何只允许列入白名单的资源(脚本,像素等)在沙盒iframe中运行? - How to allow only whitelisted resources (scripts, pixels etc.) to run within a sandboxed iframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM