简体   繁体   English

表单vs href链接损坏

[英]form vs href link corrupt

So I have been trying to get upload/download system to work but kept running into problems, specifically, the downloaded file being corrupt (.xls). 因此,我一直试图使上载/下载系统正常工作,但一直遇到问题,特别是下载的文件已损坏(.xls)。

This is what I have currently: 这是我目前拥有的:

<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>

So, these both link to the same file and downloads fine. 因此,这些都链接到相同的文件并可以正常下载。 If I click on the download file link, file opens perfectly fine. 如果单击下载文件链接,则文件打开完全正常。 If I go through the form download button, then it'll download but opening gives me a warning/error: "the file format and extension of 'file' don't match" and just hangs forcing me to force close the file. 如果我单击表单下载按钮,它将下载,但打开时会出现警告/错误:“'file'的文件格式和扩展名不匹配”,并挂起,迫使我强制关闭文件。

download_file.php: download_file.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;
}
    ?>

This is all in a Magento Static Block, in case that has anything to do with it. 万一与Magento静态块有任何关系,这些都在其中。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Have you tried this: 您是否尝试过:

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);

Greetings. 问候。

Try changing: 尝试更改:

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

to: 至:

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

Or if you use a variable: 或者,如果您使用变量:

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

Basically, start and end with a quote and don't use any quotes or apostrophes in between. 基本上,以引号开头和结尾,并且中间不要使用任何引号或撇号。

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

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