在.net网站中生成文本文件的最佳方式是什么? [英] What's the best way to generate a Text file in a .net website?

查看:156
本文介绍了在.net网站中生成文本文件的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的vb.net Web应用程序中有一个页面,需要将一堆数据折腾到文本文件中,然后将其显示给用户进行下载。在.net网络服务器上构建这样一个文本文件的最佳/最有效的方法是什么?



编辑:下面回答一个问题,这将是一个下载一次,然后丢弃一些文件。



更新:我将John Rudy和DavidK的建议粘在一起,并且工作得很好。谢谢,所有!

解决方案

答案将取决于被遗忘的分号是否需要重复下载或一次性完成抛出。



无论哪种方式,关键是设置输出的内容类型,以确保显示下载窗口。直接文本输出的问题是浏览器会尝试在自己的窗口中显示数据。



设置内容类型的核心方式是类似于以下,假设文本是输出字符串,filename是要保存文件(本地)的默认名称。

  HttpResponse response = HttpContext.Current.Response; 
response.Clear();
response.ContentType =application / octet-stream;
response.Charset =;
response.AddHeader(Content-Disposition,String.Format(attachment; filename = \{0} \,filename));
response.Flush();
response.Write(text);
response.End();

这将提示用户下载。



现在,如果您需要将文件保存在Web服务器上,那么它会变得更加棘手,但不是非常糟糕。在那里,您可以使用System.IO中的类将文本写入文本文件。确保您写入的路径可由网络服务,IUSR_MachineName和ASPNET Windows用户写入。否则,相同的交易 - 使用内容类型和标题来确保下载。



我建议不要直接保存文件,除非您需要 - 甚至是直接在服务器上这样做的技术可能不是正确的想法。 (EG,如果您需要访问控制来下载文件?如果您需要在应用程序根目录之外执行此操作,根据您的托管环境可能甚至不可能)。



所以,不知道你是一次性还是文件必须真正的保存模式,而不知道安全性的影响(如果真的需要服务器,你可能需要自己解决)边保存),这是我能给你的最好的。


I have a page in my vb.net web application that needs to toss a bunch of data into a text file and then present it to the user for download. What's the best / most efficient way to build such a text file on a .net web server?

Edit: to answer a question down below, this is going to be a download once and then throw-away kind of file.

Update: I glued together the suggestions by John Rudy and DavidK, and it worked perfectly. Thanks, all!

解决方案

The answer will depend on whether, as Forgotten Semicolon mentions, you need repeated downloads or once-and-done throwaways.

Either way, the key will be to set the content-type of the output to ensure that a download window is displayed. The problem with straight text output is that the browser will attempt to display the data in its own window.

The core way to set the content type would be something similar to the following, assuming that text is the output string and filename is the default name you want the file to be saved (locally) as.

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/octet-stream";
response.Charset = "";
response.AddHeader("Content-Disposition", String.Format("attachment; filename=\"{0}\"", filename));
response.Flush();
response.Write(text);
response.End();

This will prompt a download for the user.

Now it gets trickier if you need to literally save the file on your web server -- but not terribly so. There you'd want to write out the text to your text file using the classes in System.IO. Ensure that the path you write to is writable by the Network Service, IUSR_MachineName and ASPNET Windows users. Otherwise, same deal -- use content type and headers to ensure download.

I'd recommend not literally saving the file unless you need to -- and even then, the technique of doing so directly on the server may not be the right idea. (EG, what if you need access control for downloading said file? Now you'd have to do that outside your app root, which may or may not even be possible depending on your hosting environment.)

So without knowing whether you're in a one-off or file-must-really-save mode, and without knowing security implications (which you'll probably need to work out yourself if you really need server-side saves), that's about the best I can give you.

这篇关于在.net网站中生成文本文件的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆