[英]Using AsyncTask to download file from PHP site without any Open/Save dialog box
[英]How to download a specific CSV file from the server to the client using Save As dialog box in php?
我在服务器上有一个名为CO2.csv的文件,我希望用户通过表单中的按钮下载并使用新文件名将其保存到他的硬盘驱动器中的任何位置。
这是调用php代码的按钮的HTML脚本
<form action="FCO2.php" method="post" <?php if(empty($_SESSION["CO2"])){?>style="display:none"<?php } ?> >
<input type="submit" value="Export to CSV" name="submit">
</form>
这是FCO2.php
<?php
$file = 'temp/CO2temp.csv';
if(!$file)
{ // file does not exist
die('file not found');
}
else
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: text/csv");
}
?>
但是它没有用,它总是创建一个空文件,并且从不复制服务器上的文件。
哦,您只是错过了对实际文件的调用,例如使用readfile
因此添加到:
<?php
$file = 'temp/CO2temp.csv';
if(!$file)
{ // file does not exist
die('file not found');
}
else
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: text/csv");
readfile($file);
exit;// good practice but if this is the whole file, its not needed
}
?>
您正在输出一堆标题,但是PHP在输出文件之前结束。
您可以使用readfile来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.