Response.transmitFile()传输文件过大问题

qianquwuyong 2011-01-19 09:30:01
我有一个.xml文件,需要用Response.transmitFile(server.mapPaht("xxx.xml"))方法把.xml文件以二进制的形式发送到http流中,在浏览器上就不会显示关于.XML文件的格式内容。请大家帮帮忙,在此谢谢了!
...全文
510 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qianquwuyong 2011-07-26
  • 打赏
  • 举报
回复
谢谢各位
wanderpig 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 net_lover 的回复:]

下载大文件,你需要

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataTo……
[/Quote]

使用分块是可以下载大文件,但是你有没有想过,如果文件过大(500M),不说多了就10个人去下载这个文件,那么服务器的Cpu是否能承受?
  • 打赏
  • 举报
回复
上面的额够详细了,为什么不上网查查呢,直接问
孟子E章 2011-01-20
  • 打赏
  • 举报
回复
参见
Response.WriteFile 无法下载大文件的解决方法
http://support.microsoft.com/kb/812406/zh-cn
孟子E章 2011-01-20
  • 打赏
  • 举报
回复
下载大文件,你需要

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = "xxx.xml";

// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);


// Total bytes to read:
dataToRead = iStream.Length;

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
wuyq11 2011-01-19
  • 打赏
  • 举报
回复
Response.AppendHeader("Content-Disposition", "attachment;filename=a.xml");
Response.TransmitFile(Server.MapPath("a.xml"));
wuyq11 2011-01-19
  • 打赏
  • 举报
回复
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=a.xml");
Response.TransmitFile(Server.MapPath("LabelData.xml"));
Response.End();

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧