How to designate a downloadable file's content

2019-07-20 05:39发布

问题:

This SO answer applies to me, but I don't know how to "change the content type to application/vnd.apple.pkpass"

Currently a direct link to my pkpass triggers a download and I don't know where to set the content type.

I have asked my hosting provider to verify that "application/vnd.apple.pkpass" is a supported MIME type

I have tried

What can I do to make the file recognized as the "application/vnd.apple.pkpass" when the link is clicked from iOS 6 or Mac OS 10.8?

回答1:

You need to change the web server's configuration to do this. How you do this depends on which web server you're using. If you have a hosting company that manages your site's server for you, you likely need to ask them to do this or tell you where you can.

Many hosting companies use Apache for the web server. If so, you can create a .htaccess file in your public HTML directory, and put this in it:

AddType application/vnd.apple.pkpass .pkpass

This will make any file that ends in ".pkpass" get downloaded with that content type.

This assumes you're having the client download a static file. If you're dynamically generating this file, you don't need to mess with the server at all, and just need to send a header. This depends on which scripting language you're using, for example in PHP you'd do:

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


回答2:

If you are generating passes dynamically, you may find the following helpful to ensure that a fresh .pkpass downloads each time, and to serve the file with a last-modified header as recommended by Apple.

Written in PHP but easily ported to other languages.

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