【问题标题】:How to designate a downloadable file's content type如何指定可下载文件的内容类型
【发布时间】:2012-12-12 19:05:08
【问题描述】:

这个SO answer适用于我,但我不知道如何“将内容类型更改为application/vnd.apple.pkpass”

目前直接链接到我的 pkpass 会触发下载,我不知道在哪里设置内容类型。

我已要求我的托管服务提供商确认“application/vnd.apple.pkpass”是受支持的 MIME 类型

我试过了

当从 iOS 6 或 Mac OS 10.8 中单击链接时,如何使文件被识别为“application/vnd.apple.pkpass”?

【问题讨论】:

    标签: mobile-safari mime-types passbook


    【解决方案1】:

    您需要更改 Web 服务器的配置才能执行此操作。如何执行此操作取决于您使用的 Web 服务器。如果您有一家托管公司为您管理您网站的服务器,您可能需要请他们这样做或告诉您在哪里可以这样做。

    许多托管公司使用 Apache 作为 Web 服务器。如果是这样,您可以在公共 HTML 目录中创建一个 .htaccess 文件,并将其放入其中:

    AddType application/vnd.apple.pkpass .pkpass
    

    这将使任何以“.pkpass”结尾的文件都以该内容类型下载。

    这假设您让客户端下载一个静态文件。如果您是动态生成此文件,则根本不需要弄乱服务器,只需要发送一个标头即可。这取决于您使用的脚本语言,例如在 PHP 中您会这样做:

    header("Content-Type: application/vnd.apple.pkpass");
    

    【讨论】:

    • 这绝对有帮助。 +1 我确实问过我的托管服务提供商(他们说设置了 MIME 类型,但静态下载不起作用)我动态生成通行证,所以我正在尝试使用 rails 应用程序的标题。
    • 我能够通过 Siteworx -> Admin -> HTACCESS 为我的托管服务提供商添加 MIME 类型。谢谢
    【解决方案2】:

    如果您正在动态生成通行证,您可能会发现以下内容有助于确保每次都下载新的 .pkpass,并按照 Apple 的建议使用最后修改的标头提供文件。

    用 PHP 编写,但很容易移植到其他语言。

    // Assumes a .pkpass bundle named pass.pkpass in the same directory.
    
    //Prevent caching
    header("Pragma: no-cache");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    
    
    // Set MIME type, encoding and force download
    header("Content-type: application/vnd.apple.pkpass; charset=UTF-8");
    header('Content-Transfer-Encoding: binary');
    header('Content-Disposition:attachment; filename="pass.pkpass"');
    
    
    // Provide a content-length header based on the file size
    $filesize = filesize('pass.pkpass');
    if ($filesize)
      header("Content-Length: ". $filesize);
    
    
    // Set a last-modified header (used by the device in update requests)
    date_default_timezone_set("UTC");
    header('Last-Modified: ' . date("D, d M Y H:i:s", time())) . ' GMT');
    
    
    // Clear anything that may be in the output buffer and send the bundle contents
    flush();
    readfile('pass.pkpass');
    
    // Do any clean up required
    
    exit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多