Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MHTML File Header Redirect showing source code with MIME segments

I am having an issue getting an iframe to display MHT files:

if( strtolower( substr( $filename , -4 ) ) == ".mht" ){

    $filename = str_replace( "/" , "\\" , $filename );

    header("Content-type: $filetype");
    header("Content-Length: $filesize");
    header('Content-Disposition: attachment; filename="'.$title.'.mht"');

    readfile( HT_STORAGE . $filename );
}
else{
    $filename = str_replace( "\\" , "/" , $filename );
    header( "location: https://secure.***************.com/" . $filename );
}

The above method works, but it requires two steps (download and open) that my clients aren't happy about.

As you can see, right now I am simply forcing the browser to open the MHT as a download. I want it to redirect using a header location so that it displays the MHT within this iframe like it does for files who don't have the MHT extension. I am assuming that this is simply a header call that tells the browser that its MHT content. With a regular redirect, the browser just shows a bunch of MHT tags, no actual content (which is why I am assuming that a special header is required). Any ideas?

EDIT:

Here is some more information I found regarding this issue. The MHT file contains several segments that look like this:

From: "Saved by Windows Internet Explorer 9"
Subject: Print Preview
Date: Tue, 2 Aug 2011 12:06:51 -0500
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0186_01CC510C.A9789090"
X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7600.16807

This is a multi-part message in MIME format.

AND

------=_NextPart_000_0186_01CC510C.A9789090
Content-Type: application/octet-stream
Content-Transfer-Encoding: quoted-printable
Content-Location: https://*******.net/packages/js/jquery.header.js

For some reason, if I use the attachment methodology and "open" the file, IE/Firefox has no problem rendering the document. If I use the header "location" method, it just displays the contents of the file (html tags, mime stuff, etc...), the the interpreted content. Doings this displays the raw HTML output, rather than actually displaying the MHT page:

if( strtolower( substr( $filename , -4 ) ) == ".mht" ){

    $filename = str_replace( "\\" , "/" , $filename );

header("Content-type: message/rfc822");
header( "location: " . $filename );
//header("Content-Length: $filesize");
//header('Content-Disposition: attachment; filename="'.$title.'.mht"');

//readfile( HT_STORAGE . $filename );
}
else{
$filename = str_replace( "\\" , "/" , $filename );
header( "location: https://secure.*****************.com/" . $filename );
}

By using content-type and location, the output is as follows on IE and FF (and most likely all other browsers):

From: "Saved by Windows Internet Explorer 9"
Subject: Print Preview
Date: Tue, 2 Aug 2011 12:06:51 -0500
MIME-Version: 1.0
Content-Type: multipart/related;
    type="text/html";
    boundary="----=_NextPart_000_0186_01CC510C.A9789090"
X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7600.16807

This is a multi-part message in MIME format.

------=_NextPart_000_0186_01CC510C.A9789090
Content-Type: text/html;
    charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: https://kinnser.net/am/printwrapper.cfm?PatientTaskKey=36184728

=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>Print=20
Preview</TITLE>...

So rather than actually rendering the MHT, it just spits out all the tags and data.

Any ideas? Should I have to strip the "MIME" data off the files?:

------=_NextPart_000_0186_01CC510C.A9789090
    Content-Type: text/html;
        charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Location: https://kinnser.net/am/printwrapper.cfm?PatientTaskKey=36184728

If so, then how?

Edit

Ironically, if I set it up as content-disposition: attachment and use the readFile, I can open the document and it displays properly. Only if I try to directly inject it inline into the iframe does it render source code and no content. Any ideas?

like image 330
Andrew Rhyne Avatar asked Nov 14 '12 17:11

Andrew Rhyne


1 Answers

There is currently a known issue with Google-Chrome when viewing MHTs - it will always treat them as Content-Disposition: attachment and download them.

IE should always view MHTs fine.

Firefox will view them fine, if you install a plugin for it.

If the browser was actually able to display MHTs correctly, then the following headers should be sufficient:

Content-Type: message/rfc822
Content-Disposition: inline

I was running into issues with Chrome viewing MHT files as well, and in the end the solution I used was to convert the MHT to a single HTML page on the fly. This worked for the situation I was dealing with, but may not work for generic MHT files.

like image 192
a_m0d Avatar answered Oct 18 '22 17:10

a_m0d