在控制器方法中将 CSV 内容用<pre>标记包装起来

huangapple go评论55阅读模式
英文:

Wrap csv content in <pre> tag in controller method

问题

我需要从我的控制器方法返回一个csv文件的内容,并在iframe中通过src网址显示它。最好的方法就是将ContentType设置为text/plain,但在这种情况下,我无法控制客户端文件内容的样式(它默认进行换行,而我不希望在csv文件中看到这种情况)。

因此,我设法将csv内容包装在<pre>标签中,在服务器端传递自己的样式,并将其返回为text/html文件。有时它有效,有时我会遇到解码错误。如何修复这个问题?或者是否存在另一种解决方案?

英文:

I need to return a csv file content from my controller method and display it inside iframe by src url. The best way to do it is just to use text/plain as ContentType, but in this case I have no control over the file content styling on the client side (it makes word wrap by default, which I do not expect to see for csv file).

So I managed to wrap csv content in <pre> tag passing my own styles on the server side, and return it as text/html file. Sometimes it works, sometimes I get decoding error. How it can be fixed? Or there is another solution exists?

答案1

得分: 0

已解决。不要忘记添加:

Response.Headers.Add("Content-Encoding", Encoding.UTF8.ToString());

以避免编码问题。

英文:

Solved. Don't forget to add:

Response.Headers.Add(&quot;Content-Encoding&quot;, Encoding.UTF8.ToString());

to avoid issues with encoding

huangapple
  • 本文由 发表于 2023年6月13日 03:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定