确定html字符串的内容长度

dav*_*eps 1 html asp.net httpresponse http-headers

我通过将数据作为HTML表字符串发送并设置内容标题来导出HTML表格到excel:

Dim html as String = "<table><tr><td>example<td></tr></table>"

context.Response.Clear()
context.Response.AddHeader("Content-Disposition", "attachment; filename=" & "exceldata-" & Now.ToString("yyyyMMddHHmmss") & ".xls")
'context.Response.AddHeader("Content-Length", ????)
context.Response.ContentType = "application/octet-stream"
context.Response.Write(response)
context.Response.End()
Run Code Online (Sandbox Code Playgroud)

是否有一种基于html字符串大小设置内容长度的简单方法?或者我应该把它留空呢...理想的内容长度会很好...

我在asp.net中使用GenericHandler返回这个

Joh*_*han 5

替换为您选择的编码,可能是UTF8.抱歉C#:

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(html);
int length = bytes.Length;
Run Code Online (Sandbox Code Playgroud)

资料来源:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx