代码之家  ›  专栏  ›  技术社区  ›  opHASnoNAME

MSOffice2007/97用PHP下载的头文件

  •  0
  • opHASnoNAME  · 技术社区  · 14 年前

    我们试图通过php和zend框架将上传的附件(从数据库作为blob提供)发送到客户机。

    此代码适用于Excel97/Word97。

      if ($this->getResponse()->canSendHeaders(false)) {
                $response = $this->getResponse();
    
                $response->setHeader('Pragma', 'public', true)
                         ->setHeader('Expires', '0', true)
                         ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
                         ->setHeader('Content-Type', 'application/force-download', true)
                         ->setHeader('Content-Type', 'application/octet-stream', true)
                         ->setHeader('Content-Type', 'application/download', true)
                         ->setHeader('Content-Disposition', "attachment;filename=$filename", true)
                         ->setHeader('Content-Transfer-Encoding', 'binary', true)
                         ->setBody($data) // binary
                         ->sendHeaders();
            } 
    

    但不适用于Excel2007/Word2007。它报告“文件有错误”,并试图修复它。

    有什么建议吗?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Stefan Gehrig    14 年前

    你应该寄正确的 Content-Type 对于给定的文件格式。那应该是 application/msword / application/vnd.ms-excel 如果你把旧的 .doc -或 .xls -文件和 application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 对于新手 x 类型文件 .docx .xlsx 分别(不知道您提供的是哪个版本)。

    此外,据我所知,更新的Office程序会检查文件扩展名是否与文件内容匹配,因此在打开时会收到错误或警告 doc -文件与 多克斯 -延伸。

    ...->setHeader('Content-Type', '<<the appropriate content-type>>', true) 应该足够了。

        2
  •  0
  •   DrColossos    14 年前

    不同的mime类型: http://filext.com/faq/office_mime_types.php 至少在我有一个类似的问题的时候解决了这个问题