关于下载的问题
我有一个下载页面,但是现在同时只能一个人下载,就是如果有人在下载过程中,别人想下载就不行
代码如下,谁帮忙看看怎么改?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//完成下载以后更新下载次数
if (download())
{
int count = 0;
System.IO.StreamReader srd;
string file_path = Server.MapPath("~/count.txt");
srd = System.IO.File.OpenText(file_path);
while (srd.Peek() != -1)
{
string str = srd.ReadLine();
count = int.Parse(str);
}
srd.Close();
object obj = count;
Application["count"] = obj;
Application.Lock();
int State = 0;
State = (int)Application["count"];
State += 1;
obj = State;
Application["count"] = obj;
file_path = Server.MapPath("~/count.txt");
System.IO.StreamWriter srw = new System.IO.StreamWriter(file_path, false);
srw.WriteLine(State);
srw.Close();
Application.UnLock();
}
}
}
//文件下载
private bool download()
{
try
{
string filePath = Server.MapPath("~/download/trialfile/downpackage");
string downloadfile = System.Configuration.ConfigurationSettings.AppSettings["DownloadFile"].ToString();
string filepathname = filePath + "\" + downloadfile;
string name = HttpUtility.UrlEncode(System.Configuration.ConfigurationSettings.AppSettings["DownFileName"], Encoding.UTF8); //对中文文件名进行HTML转码
System.IO.FileInfo filename = new System.IO.FileInfo(filepathname);
FileStream myFile = new FileStream(filepathname, FileMode.Open, FileAccess.Read, FileShare.Write);
BinaryReader _binaryReader = new BinaryReader(myFile);
long startBytes = 0;
string lastUpdateTimeStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string _encodedData = HttpUtility.UrlDecode(downloadfile, Encoding.UTF8) + lastUpdateTimeStamp;
Response.Clear();
Response.Buffer = false;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", """ + _encodedData + """);
Response.AppendHeader("Last-Modified", lastUpdateTimeStamp);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
Response.AddHeader("Content-Length", (filename.Length - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentEncoding = Encoding.UTF8;
_binaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Ceiling((filename.Length - startBytes + 0.0) / 1024);
int i;
for (i = 0; i < maxCount && Response.IsClientConnected; i++)
{
Response.BinaryWrite(_binaryReader.ReadBytes(1024));
Response.Flush();
}
_binaryReader.Close();
myFile.Close();
myFile.Dispose();
if (i < maxCount) return false;
return true;
}
catch
{
return false;
}
}