MIME Types in for Server Side TiddlyWikis – Download a HTML file from a link

One of the things we like about TiddlySpot is the way that it immediately provides the save as dialog box when you click the download link. For a demo at the UC Hot House we needed to do something similar. Here is what I did it. If any one knows of a better way to achieve the same goal please let me know.

With PHP its nice and easy to return a different MIME type. This can be done using one line of code :

header(“Content-Type: application/zip zip”);

There a many different MIME types available, Wikipedia provides a summary of some of the more relevant types at :

http://en.wikipedia.org/wiki/Internet_media_type

Setting the MIME type to HTML simple loads the webpage into the browser becuase it knows how to display html files. There were two ways to produce the save dialog box, firstly the above example. By telling the browser to expect a Zip file it immediately returns the save as box. I was not overly happy with this because it was not a zip file and the dialog box informed the user they were going to be downloading a PC ZIP Achive :

picture-2.png

 

After some googling I discovered that browsers will return the save as dialog for any file formats that they do not understand. So I changed the MIME type to :

header(“Content-Type: application/zisdfsdfpsdf zisdfp”);

and got the same except it was defaulting to saying it was saving a .php file in the save dialog box.

This of course only returns an empty file: To make it save the tiddlywiki file I then added :


header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/sdfzip zipsdfsdf");

$file = @fopen('index.html',"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

The fact that it was saving a PHP file was a problem. This was actually very simple to resolve :I created a .htaccess file in the folder containing the above code and entered :

AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html

This means that any .html and .htm files will now be processed by the PHP engine. The final step it to change the filename of the file containing the above PHP code so that it is a HTML file.

The end result is the file should prompt you to save a HTML from one click.

Advertisements

5 thoughts on “MIME Types in for Server Side TiddlyWikis – Download a HTML file from a link

  1. That’s very nice!
    Just the other week I had suggested this to Jeremy and Andreas (for TiddlyWiki’s empty.html), but we couldn’t figure out how to make it work. Maybe you could bring that up again?

  2. I believe the accepted ‘standard’ (don’t know if it’s documented or not) is to use “application/octet-stream” for the content-type header to force download. To then change the filename you add a header with the name ‘content-disposition’.

    e.g.

    header(“content-disposition: attachment;filename=myfile.html”);

    hth

  3. Here is the tiddlyspot version (in ruby). Might be some tricks there you can use. Content-Length is nifty. Then your browser can show a progress bar for the download:

    puts CGI.new.header({
    ‘charset’=>’utf-8’,
    ‘Pragma’ => ‘private’,
    ‘Cache-Control’ => ‘private, must-revalidate’,
    ‘Content-Type’ => ‘text/html’,
    ‘Content-Length’ => File.size(index_html_file_name),
    ‘Content-Disposition’ => ‘attachment; filename=’+site_name+’.html’, }) +
    File.read(index_html_file_name)

  4. Simon,

    Excellent stuff. I was going to suggest a method like this to make life easier for those trying to download Ripplerap.

    Marvelous!

Comments are closed.