事情皆因一文件下载惹的祸。首先我介绍我的机器构成:文件服务器,暂叫A服务器,也就是放文件的机器。应用程序服务器,这大家都明白,就是部署程序的服务器,其它服务器就不说啦。
好,开始说问题,最初小弟,根本没去想太多的性能与服务器的承载量的问题。就写了下面两段代码
string physicalFilePath = downloadpath;
FileStream stream=null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
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);

HttpContext.Current.Response.End();
}
finally
{
stream.Close();
}

或:
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());
Response.ContentType = "application/ms-excel";
Response.WriteFile(file.FullName);
Response.End();等过几天,访问量达到几千人同时在线时,我的问题来啦,有的数据文件也比较大,有的是100M以上的,哪上面这两种方法同时挂掉,有的因为文件太大,而无法输出,有的的有时莫明其妙的弹出服务器重置这种提示,后来我又换啦一种下载方式,代码如下:


文件肯定存在,链接肯定正确

}
浙公网安备 33010602011771号