关于ASP.NET返回文件的问题

我今天发现我的程序中通过ASP.NET返回文件的程序,有很多ASF文件不能输出成功,系统提示Server Application Unavailable 开始我以为是ASF文件的问题,后来才发现是文件过大,原来ASP.NET的writeFile函数不能返回太大的文件,所以必须手工通过stream读取返回,具体程序如下:(其中的fileSpec包含文件路径)
None.gif System.IO.Stream iStream = null ;
None.gif 
byte[] buffer = new Byte[10000
];
None.gif 
int
 length;
None.gif 
long
 dataToRead;
None.gif 
string  filename  =
 System.IO.Path.GetFileName(fileSpec);
None.gif 
try

ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif  iStream 
= new
 System.IO.FileStream(fileSpec, System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
InBlock.gif  dataToRead 
=
 iStream.Length;
InBlock.gif  Response.ContentType 
=
 GetContentType(fileSpec);
InBlock.gif  Response.AddHeader(
"Content-Disposition""attachment; filename=" +
 filename);
InBlock.gif    
while (dataToRead > 0
)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
{
InBlock.gif   
if
 (Response.IsClientConnected) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
{
InBlock.gif    length 
= iStream.Read(buffer, 010000
);
InBlock.gif    Response.OutputStream.Write(buffer, 
0
, length);
InBlock.gif    Response.Flush();
InBlock.gif    buffer
= new Byte[10000
];
InBlock.gif    dataToRead 
= dataToRead -
 length;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dataToRead 
= -1
;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif 
catch  (Exception ex) 
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif
{
InBlock.gif  Response.Write(
"Error : " +
 ex.Message);
ExpandedBlockEnd.gif }

None.gif 
finally
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif  
if (iStream != null

ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
{
InBlock.gif   iStream.Close();
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif
嗯,搞定,现在系统写得差不多了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值