用stream模式下载大文件是遇到的问题?
1。由于文件比较大(一两百兆),所以要分成很多次来读写,但是除了打了SP1的IE6外,其他版本的浏览器都不认识Response.IsClientConnected,现象是用左键电击下载连接,点到保存路径对话框或已经开始下载后,再点取消,之后再重复之前的操作,到第三次时,程序不在有任何反映,必须把页面关掉重来才可以,但用下载工具下载不存在上述问题
2。下载之后的内存回收问题,我的代码现在不执行回收工作,请高手指教怎么改?
3。URL中没有给出文件路径(为了安全),现在的问题是用FLASHGET下载时显示的文件名是页面的名称,有没有办法控制下载工具中显示的文件名
我的代码如下:
//下载文件
private void Download()
{
Stream iStream=null;
if (!System.IO.File.Exists(FilePath))
{
ErrorInfo="Sorry,系统找不到您要下载的文件!";
return;
}
try
{
iStream= new FileStream(FilePath, System.IO.FileMode.Open,
FileAccess.Read,FileShare.Read);
if (this.StartPoint>0)
iStream.Seek(this.StartPoint,SeekOrigin.Begin );
FileLength=iStream.Length;
long ByteToRead = FileLength-this.StartPoint ;
// string FileName = "File"+FileID.ToString().Trim()+".wmv";
FileInfo fileinfo = new FileInfo(FilePath);
String FileName = fileinfo.Name;
try
{
int n = FileName.IndexOf('(');
int m = FileName.IndexOf(')');
int x = Convert.ToInt32(FileName.Substring(n+1,m-n-1));
FileName = FileName.Substring(0,n) + "--" + "第" + x + "集" + ".wmv";
}
catch
{
}
Response.Clear();
// Response.ContentType = "application/ms-download";
Response.ContentType = "video/wmv";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", ByteToRead.ToString());
Response.Flush();
this.WriteLogInfo(DateTime.Now.ToString() +" OK--------- 下载文件 " + FilePath +" 开始 "+ " DownloadID= " + DownloadID.ToString());
byte[] Buffer;
int BufferLength=Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["BufferLength"]);
// byte[] Buffer = new byte[BufferLength];
// byte[] Buffer = new byte[iStream.Length];
int Len=1;
Length1=Length2=0;
SleepInterval=1500;
Aborted=ClientClosed=EndOfFile=false;
// this.DBObj.UpdateDBOnBeginDownload(DownloadID,UserIP,ServerIP,DateTime.Now,out ErrorInfo);
this.CheckTimer.Enabled=true;
// while (!this.CanStopDownload(Aborted,ClientClosed,EndOfFile))
while (Len>0 && ClientClosed==false)
{
Buffer= new Byte[BufferLength];
Len = iStream.Read(Buffer, 0, BufferLength);
// if (Len>0)
// {
Response.OutputStream.Write(Buffer,0,Len);
Response.Flush();
// System.Threading.Thread.Sleep(SleepInterval);
// }
Length2+=Len;
// EndOfFile=Len<=0L;
ClientClosed=!this.Response.IsClientConnected;
// iStream.Read(Buffer,0,(int)iStream.Length);
//// Response.Clear();
// Response.BinaryWrite(Buffer);
// Response.Flush();
// Response.Clear();
}
iStream.Close();
iStream = null;
Response.End();