开发者

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

开发者 https://www.devze.com 2023-01-17 03:26 出处:网络
This question already has answers here: Close开发者_运维知识库d 12 years ago. Possible 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

精彩评论

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

关注公众号