确定html字符串的内容长度

发布于 2024-09-30 05:15:53 字数 625 浏览 2 评论 0原文

我通过将数据作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼疼热情 2024-10-07 05:15:53

替换为您选择的编码,可能是 UTF8。对于 C# 感到抱歉:

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(html);
int length = bytes.Length;

来源: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#:

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(html);
int length = bytes.Length;

Source: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx

一绘本一梦想 2024-10-07 05:15:53

这看起来太简单了,但是它不就是等于 html.Length 吗?

This seems too easy, but is it not just equal to html.Length?

快乐很简单 2024-10-07 05:15:53

我没有使用过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文