[英]how to create a text file in asp.net MVC3 using c#
我只是想問如何生成或創建文本文件,因為我想在數據庫中以文本形式顯示我的數據。
我在asp.net MVC 3中使用c#
非常感謝你! 任何答案都會被貶低。
如果您只想在將下載到用戶本地計算機的文本文件中從數據庫返回一些數據,請在Controller中創建一個Action ,如下例所示:
using System.IO;
using System.Text;
public class SomeController {
// this action will create text file 'your_file_name.txt' with data from
// string variable 'string_with_your_data', which will be downloaded by
// your browser
public FileStreamResult CreateFile() {
//todo: add some data from your database into that string:
var string_with_your_data = "";
var byteArray = Encoding.ASCII.GetBytes(string_with_your_data);
var stream = new MemoryStream(byteArray);
return File(stream, "text/plain", "your_file_name.txt");
}
}
然后你可以在View上為該動作創建一個ActionLink ,它將觸發文件下載:
@Html.ActionLink("Download Text File", "CreateFile", "SomeController ")
我希望有所幫助!
您可以通過組合字符串並返回Content(textString, "text/plain")
從操作返回純文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.