简体   繁体   English

文件下载iframe绝对与相对地址

[英]File download iframe absolute vs relative address

We are using this code in javascript for downloading files via iframe and it works well: 我们正在javascript中使用以下代码通过iframe下载文件,并且效果很好:

function initDownload(){
var path = "path/to/song.mp3", name = "song.mp3";
document.getElementById('myiframe').src = "dl.php?path="+path+"&name="+name;
}

<iframe id="myiframe" style="position: absolute; left: -10000px; display: none;" src=""></iframe>
<a href="#" onClick="initDownload(); return false;">download</a> 

Here is dl.php file: 这是dl.php文件:

<?php 
header("Content-Type: application/octet-stream");
if(isset($_GET['name'])){
    $name = $_GET['name'];
    header("Content-Disposition: attachment; filename=\"$name\"");
}
if(isset($_GET['path'])){
   $path = str_replace(' ', '%20', $_GET['path']);
   readfile($path);
}
exit;

?> ?>

Song path is set as absolute url and this works well. 歌曲路径设置为绝对URL,这很好用。 There have been a case when this havent worked, and after some testing, it turned out that only relative url was accepted (worked) on that particular server. 在某些情况下,这种方法还没有奏效,经过一些测试,结果表明在该特定服务器上仅接受(工作)了相对URL。 Is there a reason why an absolute url wouldnt worked on some servers, maybe some php setting or something? 有一个为什么绝对URL无法在某些服务器上工作的原因,也许是某些php设置之类的原因?

You should check, what you are doing in dl.php file with path, here html and javascript will work on all servers the same way. 您应该检查dl.php文件中包含路径的内容,此处的htmljavascript将以相同的方式在所有服务器上运行。 Quite shure the problem is in the way php getting to the file. 很确定问题在于php获取文件的方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM