我有一个嵌入图像的 MHTML 文件。MHTML 在服务器上生成,然后我通常会使用 BinaryWrite 传递文件。在转换为 ASCII 并将文件写入磁盘并使用 Response.WriteFile 之后,我也尝试过 Server.Transfer、Response.Write。在任何这些情况下,生成的文件都不会(看起来)被视为 mht 文件。为了设置图像,我尝试了 Content-ID 和 Content-Location。图像 URL 显示为cid:example1
在 IE8 中查看时的样子。保存到磁盘后打开文件时,它显示为mhtml:file://C:\Documents
和Settings\benjynito\Desktop\output634172401776447258.mht!cid:example1
。或者在使用您获得的一种有效方法进行浏览时mhttp://...output123.mht!cid:example1
。
Output.MimeType 是 message/rfc822。我也尝试过 application/octet-stream 和 multipart/related。
将文件写入磁盘并使用 Response.Redirect 有效。使用直接 URL 访问文件有效。将文件保存到磁盘然后打开文件即可。
似乎 IE 正在为请求假设 HTML 结果,而不是破译新的内容类型。但是你可以为动态样式表、脚本等做这样的事情......所以我真的不相信。我看不出任何明显的差异。我刚刚尝试过,BinaryWrite 在 Opera 中运行良好。
如果我绝对要担心写入临时目录然后重定向到文件,我会的。我只是希望避免清理临时文件。我想做的事是不可能的吗?下面是一个写入文件的示例。
提前致谢!
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();
}