【问题标题】:docx and excel downloaded files giving errordocx 和 excel 下载的文件出现错误
【发布时间】:2018-12-05 12:21:23
【问题描述】:

我已经从我的 windows 10 > office 2007 创建了新的 docx、xlsx 和 xls 文件。我在其中只写了一个单词“testing123”。我正在使用以下代码下载文件。

$file = storage_path("app/".$directory_name."/".$filename);
$headers = array(
    'Content-Type: ' . mime_content_type( $file ),
);
return response()->download($file, $filename ,$headers);

我也尝试静态使用以下内容类型。

'application/octet-stream', // txt etc
    'application/msword', // doc
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
    'application/vnd.ms-excel', // xls
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // xlsx
    'application/pdf', // pdf

但我收到以下错误。

Excel 无法打开文件 testing.xlsx,因为文件格式或 文件扩展名无效。确认该文件未被 已损坏且文件扩展名与文件格式匹配。

在这种情况下有什么解决方案?有什么建议吗?

【问题讨论】:

    标签: php laravel-5.4


    【解决方案1】:

    您可能需要添加以下内容: header("Content-disposition: attachment; filename=\"" . $filename . "\"");

    【讨论】:

      【解决方案2】:

      您在 Excel 中遇到错误 - 不是在您的浏览器或应用程序中,因此下载可能工作正常,问题在于文件的内容及其创建方式。下一步是通过检查下载的内容文件与存储在服务器上的内容文件来验证这一点。

      【讨论】:

      • 我没有使用任何编程语言动态创建它。它是静态文件。我正在使用 Microsoft Office 2007 创建它。我在这个文件中只写了一个字。
      • 没关系。您仍然需要检查文件是否被截断或以某种方式转换。
      猜你喜欢
      • 2021-04-09
      • 2022-06-14
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多