将文件写入虚拟服务器,ASP.NET

发布于 2024-09-24 15:52:54 字数 304 浏览 2 评论 0原文

可能的重复:
写入 CSV 文件并导出?

这可能是一个愚蠢的问题我已经在 Google 上搜索过,但无法找到明确的答案 - 如何将 CSV 文件写入网络服务器并在 C# ASP.net 中将其导出?我知道如何生成它,但我想将其保存到 www.mysite.com/my.csv,然后导出。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2024-10-01 15:52:54

您不需要将 CSV 文件保存到磁盘,特别是如果它是动态生成的。您可以直接将其写入响应流,以便用户可以下载它:

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

您还可以编写一个 http 处理程序:

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

您可以像这样调用它:http://mysite.com/csvhandler.ashx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文