【发布时间】:2021-03-26 09:45:21
【问题描述】:
我正在尝试在我的网络服务器上添加代码以从我们的远程网站下载文件。 (我只是以微软下载文件为例)
<p>
<script type = "text/javascript">
document.write('<iframe src="https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi" size="0" width="0" height="0" style="display: none"></iframe>');
document.write('<table><tr>');
document.write('<td><span style="font-weight: normal; font-size: 20pt; color: #666666;">Thank you for downloading</span></td><br />');
document.write('</tr></table>');
</script>
</p>
此 iframe 不起作用。我也试过了:
<p>
<script type = "text/javascript">
function download(file, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;base64, ' + encodeURIComponent(text));
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
document.write('<table><tr>');
document.write('<td><span style="font-weight: normal; font-size: 20pt; color: #666666;">Thank you for downloading</span></td><br />');
document.write('</tr></table>');
var text = "any text";
var filename = "https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi";
download(filename, text);
</script>
</p>
但这也不起作用。我已经尝试过Client download of a server generated zip file建议的文章/代码@任何人有我可以使用的示例或者上面的代码有什么问题?
【问题讨论】:
-
谢谢,但这对我不起作用。我试过了。
-
你的意思是它不起作用。请注意,基本上在任何问答网站上说“它不起作用”都是非法的
-
我的意思是浏览器从来没有要求保存下载的文件。
-
您收到错误了吗?
标签: javascript download