写入文件"关于即时"用C#客户端 [英] Write a file "on-the-fly" to the client with C#

查看:128
本文介绍了写入文件"关于即时"用C#客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和ASP.NET 2.5。

I'm using C# and ASP.NET 2.5.

我想要一个简单的方法来生成即时文件(让我们说这个例子CSV文件),并将其传送给客户端,而实际上它写入服务器文件系统。

I want a simple way to generate a "file" on-the-fly (let's say a csv file for this example) and transmit it to the client without actually writing it to the server file system.

推荐答案

一些搜索和反复试验后,我制定了以下。这似乎正好符合要求。它应该是很容易适应PHP或任何其他服务器端软件,因为它主要涉及到修改的标题。

After some searching and trial and error, I developed the following. It seems to fit the bill exactly. It should be very easily adaptable to PHP or any other server-side software since it mostly involves modifying headers.

protected void streamToResponse()
{
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=testfile.csv");
    Response.AddHeader("content-type", "text/csv");

    using(StreamWriter writer = new StreamWriter(Response.OutputStream))
    {
        writer.WriteLine("col1,col2,col3");
        writer.WriteLine("1,2,3");
    }
    Response.End();
}

这篇关于写入文件"关于即时"用C#客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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