如何使用C#创建在asp.net MVC3一个文本文件 [英] how to create a text file in asp.net MVC3 using c#

查看:126
本文介绍了如何使用C#创建在asp.net MVC3一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想问如何生成或创建文本文件,监守我想在数据库中的文本显示我的数据。

I just wanna ask how to generate or create textfile, becuase i want to display my data in the database as text.

使用C#在asp.net MVC 3 IM

im using c# in asp.net MVC 3

非常感谢你!任何回答将是preciated。

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:

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");   
    }

}

,那么你可以创建一个 ActionLink的以在该操作您的查看将触发文件下载:

then you can create an ActionLink to that action on your View which will trigger file download:

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

我希望帮助!

这篇关于如何使用C#创建在asp.net MVC3一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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