C#Asp.net文件写入客户端 [英] C# Asp.net write file to client

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

问题描述

我需要写一些报告,然后让用户提示将其保存到他/她的本地机器上。我最后一次这样做,我写一个文件到Web服务器,然后通过响应对象时,它发送到客户端。

I need to write some reports and then have the user prompted to save it to his/her local machine. The last time I did this I wrote a file to the webserver and then sent it to the client via Response object.

在其上创建Web服务器

to create on the webserver

TextWriter tw = new StreamWriter(filePath);

要发送到客户端

page.Response.WriteFile(path);

现在的问题是,有没有办法跳过物理文件到web服务器的写入和从对象重新present文档,以响应右走?

The question is, Is there any way to skip the writing of the physical file to the webserver and go right from an object that represent the document to the response?

推荐答案

您可以使用Response.ContentType这样

You could use the Response.ContentType like this

Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.txt");

如果你想要写一个文本文件,这当然起作用。如果你想要写,例如,你改变的ContentType一个.doc为应用程序/ msword等...

This of course works if you want to write a text file. In case you want to write a .doc for example you change the ContentType to "application/msword" etc...

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

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