简体   繁体   English

如何使用c#在asp.net MVC3中创建文本文件

[英]how to create a text file in asp.net MVC3 using c#

I just wanna ask how to generate or create textfile, becuase i want to display my data in the database as text. 我只是想问如何生成或创建文本文件,因为我想在数据库中以文本形式显示我的数据。

im using c# in asp.net MVC 3 我在asp.net MVC 3中使用c#

thank you very much! 非常感谢你! any answer will be apreciated. 任何答案都会被贬低。

If you just want to return some data from the database in a text file that will be downloaded to user's local computer, create an Action in your Controller like in this sample: 如果您只想在将下载到用户本地计算机的文本文件中从数据库返回一些数据,请在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");   
    }

}

then you can create an ActionLink to that action on your View which will trigger file download: 然后你可以在View上为该动作创建一个ActionLink ,它将触发文件下载:

@Html.ActionLink("Download Text File", "CreateFile", "SomeController ")

I hope that helps! 我希望有所帮助!

您可以通过组合字符串并返回Content(textString, "text/plain")从操作返回纯文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM