有关在ASP.NET中下载文本文件的问题? [英] Problem about downloading a text file in ASP.NET?

查看:70
本文介绍了有关在ASP.NET中下载文本文件的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我应该动态创建一个.txt文件.创建文本文件后,用户应下载该文件.为此,我尝试使用代码(我基本上简化了代码,如下所示):

Hi ,

I should create a .txt file dynamically . After the text file is created, it should be downloaded by the user. For this purpose , I try the code (I simplified the code basically like below ):

string blabla= "Hello World";
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Exported text File", "attachment; filename=helloWorld.txt");
Response.ContentEncoding = Encoding.Default;
byte[] bytes = Encoding.Default.GetBytes(blabla);
Response.BinaryWrite(bytes);
Response.End();


但是,此代码块的行为类似于Response.Write().内容写在下载页面上.没有要下载的文件.


But this code block behaves like Response.Write() . The content is written at the download page. There is no file to be downloaded.

Thanks for the replies in advance..

推荐答案

尝试在标题中添加Content-Disposition.
试试这个:
Try adding Content-Disposition in header.
Try this:
string blabla = "Hello World";
Response.Clear();
Response.ContentType = "text/plain";
//Response.AddHeader("Exported text File", "attachment; filename=helloWorld.txt");
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=helloWorld.txt"));
Response.ContentEncoding = Encoding.Default;
byte[] bytes = Encoding.Default.GetBytes(blabla);
Response.BinaryWrite(bytes);
Response.End();





--Amit





--Amit


这篇关于有关在ASP.NET中下载文本文件的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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