使用 ASP.NET 将 HTML 转换为 MHTML






4.06/5 (13投票s)
2004年6月15日
1分钟阅读

381045

4542
关于如何将包含图像的 HTML 文档转换为 mhtml 文档的文章。
引言
您是否希望将 HTML 文档转换为报告,并将其发送给客户端以便在 Word 或 Excel 中离线使用?符合 RFC 标准的多部分 MIME 消息(mhtml web 存档)是一个单独的文件,其中包含所有相关材料,例如链接的文档和图像,这些材料被序列化为它们的 Base64 内联编码表示形式。 .NET 中没有原生支持创建 mhtml 存档,但借助 Windows CDO 库,可以轻松实现。
代码
该项目包含 3 个类:mht
、mhtImage
和 mhtImageCollection
。 mht
类包含转换函数,例如 convertWebControlToMHTString
,它接收一个 webControl 和一个图像集合,并返回创建的 mht 存档的字符串表示形式。当转换的 webControl 依赖于用户特定的 Session 和 Application 变量进行渲染时,或者当您使用动态创建的图像时,请使用此函数。
Public Function convertWebControlToMHTString(ByVal control As WebControl, _
ByVal MHTimages As mhtImageCollection) As String
'Render WebControl to html
Dim html As String = getHtml(control)
'If WebControl has images, make the html Word compatible
If Not MHTimages Is Nothing Then
fixImageLocation(html, MHTimages)
End If
Dim msg As New CDO.MessageClass
Dim stm As ADODB.Stream = Nothing
Dim MS As System.IO.MemoryStream = Nothing
Dim iBp As CDO.IBodyPart
'Make a multipart mhtml document
Dim mainBody As CDO.IBodyPart
mainBody = msg
mainBody.ContentMediaType = "multipart/related"
'Make the html part of the document
iBp = mainBody.AddBodyPart()
iBp.ContentMediaType = "text/html"
iBp.ContentTransferEncoding = "quoted-printable"
stm = iBp.GetDecodedContentStream
stm.WriteText(html)
stm.Flush()
'Make the image parts of the document
If Not MHTimages Is Nothing Then
Dim oMhtImage As mhtImage
For Each oMhtImage In MHTimages
iBp = mainBody.AddBodyPart()
With iBp
.ContentMediaType = "image/" + _
oMhtImage.ImageFormat.ToString().ToLower()
.ContentTransferEncoding = "base64"
'ContentLocation must be the same as in the
'html part to make them linked
.Fields.Append("urn:schemas:mailheader:content-location", _
DataTypeEnum.adBSTR, , , oMhtImage.ContentLocation)
.Fields.Update()
.Fields.Refresh()
End With
Try
MS = New System.IO.MemoryStream
oMhtImage.Image.Save(MS, oMhtImage.ImageFormat)
Dim bytearray As Byte() = MS.ToArray()
stm = iBp.GetDecodedContentStream
stm.Write(bytearray)
stm.Flush()
Finally
MS.Close()
stm.Close()
End Try
Next
End If
stm = mainBody.GetStream()
Return stm.ReadText(stm.Size)
End Function
convertWebPageToMHTString
函数将特定 URL 的 HTML 文档转换为 mht 存档,包括所有图像。对于不依赖于用户特定的 Session
和 Application
变量的公共 HTML 文档,请使用此函数。
Public Function convertWebPageToMHTString(ByVal url As String) As String
Dim msg As New CDO.MessageClass
Dim stm As ADODB.Stream = Nothing
Try
msg.MimeFormatted = True
msg.CreateMHTMLBody(url, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")
stm = msg.GetStream()
Return stm.ReadText(stm.Size)
Finally
stm.Close()
End Try
End Function
fixImageLocation
在每个 ContentLocation
的开头附加字符串“http://”,如果尚未存在,则为了与 Word 兼容。
Private Sub fixImageLocation( _
ByRef html As String, ByRef MHTimages As mhtImageCollection)
Dim curContentLocation As String
Dim curIndex As Integer
Dim oMhtImage As mhtImage
For Each oMhtImage In MHTimages
curContentLocation = oMhtImage.ContentLocation
If curContentLocation.IndexOf(":") = -1 Then
curIndex = html.IndexOf(curContentLocation)
While curIndex <> -1
html = html.Insert(curIndex, "http://")
curIndex = html.IndexOf(curContentLocation, curIndex + _
curContentLocation.Length)
End While
oMhtImage.ContentLocation = "http://" + curContentLocation
End If
Next
End Sub
mhtImage
类包含图像信息。属性 Image
包含实际图像。属性 ContentLocation
包含图像的路径,必须完全与 HTML 部分中图像的源相同。属性 ImageFormat
包含图像格式(jpg、gif、bmp...)。
mhtImageCollection
类包含一个 mhtImages 集合。
使用代码
一个包含单个图像的 Panel webControl 如何创建 mht 存档的示例。
Dim oMhtCol As New mhtImageCollection
oMhtCol.add(New mhtImage(System.Drawing.Image.FromFile( _
Server.MapPath("/mhtml/images/myComputer.jpg")), _
"images/myComputer.jpg", System.Drawing.Imaging.ImageFormat.Jpeg))
sendMHTFile(ConvertWebControlToMHTString(Panel1, oMhtCol), "myFirstMht.mht")