英文:
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("Content-Encoding", Encoding.UTF8.ToString());
to avoid issues with encoding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论