BinaryWrite of MHT file is not treated as MHT by IE
I have an MHTML file which has embedded images. The MHTML is generated on the server and then I will typically deliver the file using a BinaryWrite. I've also tried Server.Transfer, Response.Write after converting to ASCII and writing the file to disk and using a Response.WriteFile. In any of these cases the resulting file is not (it appears) treated as an mht file. For setting the image, I've tried Content-ID and Content-Location. The image URL shows up as cid:example1
when viewed in IE8. When opening up the file after sav开发者_如何学编程ing to disk it shows up as mhtml:file://C:\Documents
and Settings\benjynito\Desktop\output634172401776447258.mht!cid:example1
. Or while browsing with one of the methods that work you get mhttp://...output123.mht!cid:example1
.
The Output.MimeType is message/rfc822. I've also tried application/octet-stream and multipart/related.
Writing the file to disk and using a Response.Redirect works. Accessing the file with a direct URL works. Saving the file to disk and then opening the file works.
It seems IE is assuming an HTML result to the request and not deciphering the new content type. But you can do things like this for dynamic style sheets, scripts, etc... so I don't really believe that. I couldn't see any glaring differences. I just tried and the BinaryWrite works fine in Opera.
If I absolutely have to worry about writing to a temporary directory and then redirecting to the file I will. I was just hoping to avoid having to clean up the temporary files. Is what I want to do not possible? An example of writing the file is below.
Thanks in advance!
if (response != null && response.Output != null)
{
Response.Clear();
Response.AddHeader("Content-Type", response.Output.MimeType);
Response.AddHeader("Content-Disposition", "attachment;filename=output" + DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture) + "." + response.Output.Extension);
// Response.Write( System.Text.Encoding.ASCII.GetString(response.Output.Bytes));
Response.BinaryWrite(response.Output.Bytes);
//Response.Clear();
//Server.Transfer("/ISV/Forms/Test/output634172397522707394.mht");
//Response.Clear();
//Response.WriteFile( Server.MapPath("/ISV/Forms/Test/output634172397522707394.mht"));
Response.Flush();
Response.End();
}
I don't think your Content-Type and Content-Disposition headers are correct. Use Fiddler to see what they're set to when you download MHTML as a static file.
Try a type of multipart/related and a disposition of inline. There's some evidence that this combination works.
精彩评论