开发者

Writing a file to the virtual server, ASP.NET [duplicate]

This question already has answers here: Close开发者_运维知识库d 12 years ago.

Possible Duplicate:

Write to CSV file and export it?

This is probably a silly question and I have searched on Google but I'm not able to find a definitive answer - how do you write a CSV file to the webserver and export it in C# ASP.net? I know how to generate it but I would like to save it to www.mysite.com/my.csv and then export it.


You don't need to save the CSV file to disk especially if it is dynamically generated. You could directly write it to the response stream so that the user can download it:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "text/csv";
    Response.AppendHeader("Content-Disposition", "attachment; filename=foo.csv");
    Response.Write("val1,val2,val3");
}

You could also write an http handler:

public class CsvHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=foo.csv");
        context.Response.ContentType = "text/csv";
        context.Response.Write("val1,val2,val3");
    }

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

which you would call like this: http://mysite.com/csvhandler.ashx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜