确定html字符串的内容长度
我通过将数据作为 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()
是否有一种根据 html 字符串的大小设置内容长度的简单方法?或者我应该将其保留为空白...理想情况下内容长度会很好...
我在 asp.net 中使用 GenericHandler 返回它
I am exporting a HTML table to excel by sending the data as a HTML Table string and setting the content headers:
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()
Is there a simple way of setting the content-length based on the size of the html string? Or should I just leave it blank anyway...would be nice to have the content-length ideally...
I am returning this using a GenericHandler in asp.net
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替换为您选择的编码,可能是 UTF8。对于 C# 感到抱歉:
来源:http://msdn。 microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx
Replace with the encoding of your choice, probably UTF8. Sorry for the C#:
Source: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx
这看起来太简单了,但是它不就是等于 html.Length 吗?
This seems too easy, but is it not just equal to html.Length?
我没有使用过 ASP.NET,但我猜想 Length() 方法返回的是字符长度,而不是字节长度,因此如果您的服务器使用 UTF-8 或 Unicode 来提供页面,则该方法将不起作用。
正如另一个答案中所述,只需让服务器为您填写即可。如果您考虑一下,当您从 ASP 生成 HTML 页面时不必添加它,因为 Web 服务器会根据 ASP 模块的响应生成它。
I haven't used ASP.NET, but I guess that the Length() method returns the length of the string in chars, not bytes, so it won't work if your server uses UTF-8 or Unicode for serving the pages.
As noted in another answer, just let the server fill it for you. If you think about it, you don't have to add it when you generate HTML pages from ASP since the web server would generate it based in the response from the ASP module.