punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Magnetic Monks, When downloading a file via Perl, I figure I should be able to force the user's machine to start up an MS Office app by preceeding the file with the appropriate MIME type disposition header.

In the past, when I downloaded, for example, a PowerPoint file, I preceeded it with

print "Content-type: application/vnd.ms-powerpoint\n"; print "Content-Disposition: attachment; filename=$filename\n\n";
and it always opened in PowerPoint. But I was actually downloading PowerPoint files.

Today, I want to download what is really an HTML file but force it to open in PowerPoint, like this:

$my_html_file = "some_file.htm"; open(FILE, $my_html_file) or dienice("cannot open file $my_html_file : + $_[0] $!"); @LINES = <FILE>; close(FILE); $filename = "thetest.ppt"; print "Content-type: application/vnd ms-powerpoint\n"; print "Content-Disposition: attachment; filename=$filename\n\n"; for $i (0..$#LINES) { print $LINES[$i]; }
but the file is just being displayed in the browser - it's completely ignoring the dispositoin header. (MSIE)

Am I doing something wrong, or expecting the impossible?

Thanks.

Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Sending MIME type headers not forcing desired behaviour.
by brian_d_foy (Abbot) on Apr 05, 2005 at 21:12 UTC

    IE ignores the Content-type if it can infer the type from the message body. Microsoft alternately says it does this only for a missing content-type or that it always does it, depending on what part of its docs you read. It also depends on which version of the browser you use and which service packs you have installed.

    You can find a lot more information on Google. :)

    --
    brian d foy <brian@stonehenge.com>
Re: Sending MIME type headers not forcing desired behaviour.
by Tanktalus (Canon) on Apr 05, 2005 at 21:18 UTC

    I'd presume it's the lack of '.' in your content type.

    print "Content-type: application/vnd.ms-powerpoint\n";
    Try substituting that and let us know how it goes ;-)

Re: Sneding MIME type headers not forcing desired behaviour.
by punch_card_don (Curate) on Apr 05, 2005 at 22:22 UTC
    Thanks all.

    The ".htm" is only in the extension of the file being read. The filename being sent is set to ".ppt".

    Adding in the '.' in the content-type header had no effect.

    Removing the "<html>" tags from the html file had no effect.

    The IE links are excellent, and contradictory. I'd like to cling to:

    Mimesniffing doesn't mean that there is no way for a server to control how a file is handled. <Content-disposition: attachment; filename="filename.ext"> header enables a server to do precisely that. With this however, IE makes sure that the user gets prompted with the file type information (based on the extension specified by the server here) before the file is opened/saved.

    but the current experience is proving that hope false.

    Forget that fear of gravity,
    Get a little savagery in your life.

      You might also have luck by trying to fake things out with PATH_INFO. We're straying off-topic here, but here goes: start with the URL you have right now:

      http://www.example.com/foo/powerpoint.html

      Tack on the end more stuff. The browser won't know which part of the path is the file name and which part is the PATH_INFO, although the server figures it out when it does its path walk.

      http://www.example.com/foo/powerpoint.html/no_really_i_mean _it.ppt

      If the browser gets to the point where it's going to decide the type based on the URL (or choose a file name to save it as), then this sometimes works. That trick is especially handy when a script returns the content, but you don't want the script name as the file name.

      http://www.example.com/cgi-bin/make_ppt.cgi/no_really_i_mean _it.ppt

      The trick is to use a lot of techniques all at the same time.

      Another piece of advice you didn't ask for: even though you are targetting IE, you might consider developing with a browser like Firefox. When you do that, you have a better chance of figuring out if its the server or the browser that's wrong. You still develop with IE, but the other browser is a check: if the server is wrong, it should (should!) be wrong in both browsers.

      I really don't miss my days as a web developer, when we had to support WebTV too. :)

      Good luck :)

      --
      brian d foy <brian@stonehenge.com>
Re: Sneding MIME type headers not forcing desired behaviour.
by dynamo (Chaplain) on Apr 05, 2005 at 21:13 UTC
    Without a custom browser or extension on the user side, I'd call it expecting the impossible. HTTP allows you to tell the browser what you are sending it, and browsers come with the ability to open external apps to open documents that they _can't_handle_natively_. The browser _can_ handle the html here, well enough so to see through your HTTP Header ruse and recognize that you are sending html (Most likely, it's the '.htm' that's tipping it off -- change that and you'll have a better chance, but no guarantees.) Perhaps there's some file extension or mime-type that describes a powerpoint-specific html file?

      Actually, you'd be surprised at what settings some browsers will let you make. It's a couple of days late for April Fool's now, but I know earlier versions of IE let you specify Netscape as the handler for HTML.

      (I'd say this was somewhere near the IE3 days, just based on where I was working, when I did this for April fool's to my co-workers computer)

      Anyway, I'd suggest looking at Microsoft's documentation on MIME type detection. Unfortunately, in their efforts to 'fix' badly written CGIs so they work in IE (giving the bad programmers less reason to fix their sloppy code), they've made it so that there are times when the content-type headers are ignored. (it's much like the 'fix' that allowed FrontPage and MS Word's 'Save as HTML' to keep generating crappy code, that looked just fine ... so long as you were using IE).

      Anyway, test with some other browsers, and see if it's just an IE-ism. If it's affecting other browsers too, dump the full headers, and see if there's anything in there that might be bad (like two Content-type headers).