[英]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.