事情皆因一文件下载惹的祸。首先我介绍我的机器构成:文件服务器,暂叫A服务器,也就是放文件的机器。应用程序服务器,这大家都明白,就是部署程序的服务器,其它服务器就不说啦。
好,开始说问题,最初小弟,根本没去想太多的性能与服务器的承载量的问题。就写了下面两段代码
string physicalFilePath = downloadpath;
FileStream stream=null;
try
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
![](/Images/OutliningIndicators/InBlock.gif)
int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode(System.IO.Path.GetFileName(Server.UrlEncode(filename))));
HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
HttpContext.Current.Response.End();
}
finally
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
stream.Close();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
或:
downloadpath为变量
System.IO.FileInfo file = new System.IO.FileInfo(downloadpath);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode(filename));
Response.AddHeader("Content-Length", file.Length.ToString());
![](/Images/OutliningIndicators/None.gif)
Response.ContentType = "application/ms-excel";
Response.WriteFile(file.FullName);
Response.End();刚开始放上去时,根本没有任何问题,因为访问量与数据文件都不够大。
等过几天,访问量达到几千人同时在线时,我的问题来啦,有的数据文件也比较大,有的是100M以上的,哪上面这两种方法同时挂掉,有的因为文件太大,而无法输出,有的的有时莫明其妙的弹出服务器重置这种提示,后来我又换啦一种下载方式,代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
#region
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
// int sleep = 200; //每秒5次 即5*10K bytes每秒 1024000
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Response.StatusCode = 206;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] range = _Request.Headers["Range"].Split(new char[]
{'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < maxCount; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_Response.IsClientConnected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i=maxCount;
}
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
br.Close();
myFile.Close();
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
return true;
}
#endregion这是从网上看到一位同仁写的,就拿过来一试,发现对大文件下载,还真不错,能顺利下载,好一段时间都没问题,心想这下可好啦,但没成想,好日子没几天,问题又接二连三的回来啦,这次的问题更莫明其妙,有的是下载到99%时,突然提示不能下载,有的呢,却一点击下载,弹出框后,就一点进度都还没有时,就一会儿弹出来一个连接超时,服务器重置的提示,把我给郁闷的,但同时呢,其它文件又可以下载用同一段代码。郁闷中ing....希望有哪位能指点一下。
文件肯定存在,链接肯定正确