问题与Response.OutputStream.Write生成的文件中添加HTML code [英] issue with Response.OutputStream.Write adding html code in the resulting file

查看:382
本文介绍了问题与Response.OutputStream.Write生成的文件中添加HTML code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文本文件中读取并尝试将其推到浏览器,提示用户下载,但我得到我的数据加上HTML code里面的文件。什么是我搞砸了?谢谢你。

 字节[] eftTextFile = ...调用返回一个字节数组的方法(做在一个TXT文件File.ReadAllBytes)

然后我尝试:

  Response.Clear();
Response.ContentType =应用程序/八位字节流;
Response.AddHeader(内容处置的String.Format(附件;文件名= \\{0} \\,文件名));
Response.AddHeader(内容长度,eftTextFile.Length.ToString());
Response.OutputStream.Write(eftTextFile,0,eftTextFile.Length);
Response.Flush();

这是我的aspx.cs文件并点击一个按钮的结果。这是一个简单的:

 < ASP:按钮的ID =的BtnCreate文本=创建=服务器的OnClick =btnCreate_Click>


解决方案

就在年底写

 到Response.End();

I read in a text file and try to push it to the browser to prompt a user to download but I'm getting my data plus HTML code inside the file. What am I screwing up? Thanks.

byte[] eftTextFile = ...calls a method that returns a byte array (does a File.ReadAllBytes on a txt file)

Then I try:

Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", fileName));
Response.AddHeader("Content-Length", eftTextFile.Length.ToString());
Response.OutputStream.Write(eftTextFile, 0, eftTextFile.Length);
Response.Flush();

This is my aspx.cs file and is the result of a button click. which is a simple:

<asp:Button ID="btnCreate" Text="Create" runat="server" OnClick="btnCreate_Click">

解决方案

Just write at the end

Response.End();

这篇关于问题与Response.OutputStream.Write生成的文件中添加HTML code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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