开发者

Dynamically write table to file and allow download via link

I have some information I am pulling from my db into a table. Can anyone direct me to a tutorial that shows how to dynamically create a file and allow a user to download that file with the table开发者_开发知识库 contents. All this must be dynamic. Can anyone help me?

Thanks in advance :)


generate a dbml file using your database. See this article - http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

create a generic handler. do this inside of the handler ProcessRequest event:

public class MyData : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {   

        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("Content-Disposition", "filename=\"myData.txt\"");   

        using(var db = new DbDataContext) {

            var data = db.Table.ToList(); //add Where(), OrderBy(), etc to filter and sort your data

            string fieldDelimeter = "\t";
            foreach(var item in data) {

                context.Response.Write(item.Field1 + fieldDelimeter);
                context.Response.Write(item.Field2 + fieldDelimeter);
                context.Response.Write(item.Field3 + fieldDelimeter);
                context.Response.Write(item.Field3 + fieldDelimeter);
                context.Response.Write(Environment.NewLine);
            }
        }
    }
}


You could write an ASHX handler. In the handler, simply read the data from your database, and output it directly as text.

eg taken from the link:

public class RSSHandler : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {   
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;

        string sXml = BuildXMLString(); 

        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Write( sXml );
    }

    public bool IsReusable
    {
        get { return true; }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜