[英]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>
這是沙盒 iframe,您不能修改它並添加“允許下載”或任何沙盒屬性。 你需要 Javascript 來解決這個問題。
當我使用這些屬性制作 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' 是 iframe 中沙盒屬性的新引入值。
sandbox="allow-downloads"
參考: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#browser_compatibility
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.