0

所以我一直试图让上传/下载系统工作,但一直遇到问题,特别是下载的文件损坏(.xls)。

这是我目前拥有的:

<form action="{{block type=" core/template" name="my-template"
template="php/download_file.php" }}" method="post">
<label for="date">Date:</label>
<input type="date" name="date" id="date"><br>
<input type="submit" name="submit" value="Download">
</form>
<a href="link/to/file">download file</a>

因此,它们都链接到同一个文件并且下载正常。如果我单击下载文件链接,文件将完全正常打开。如果我通过表单下载按钮,它会下载但打开会给我一个警告/错误:“'文件'的文件格式和扩展名不匹配”并且只是挂起迫使我强制关闭文件。

下载文件.php:

<?php
if($_POST['submit']) {

$file = 'excel_file.xls';
// Magento file path
$path = Mage::getBaseUrl('media') . 'folder' . DS . $file;

header("Content-Type: application/vnd.ms-excel");
//header("Content-Type: application/octet-stream");
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename = 'filename'");
echo $path;
exit;
}
    ?>

这一切都在 Magento 静态块中,以防万一。任何帮助表示赞赏。谢谢。

4

2 回答 2

0

你有没有试过这个:

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");  //File name extension was  wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

问候。

于 2013-08-12T23:45:34.937 回答
0

尝试改变:

header("Content-Disposition: attachment; filename = 'filename'");

至:

header("Content-Disposition: attachment; filename=filename.xls");

或者,如果您使用变量:

header("Content-Disposition: attachment; filename=$filename.xls");

基本上,以引号开头和结尾,并且不要在其间使用任何引号或撇号。

于 2013-08-14T01:35:12.827 回答