支持1G超大文件下载并及时删除

	var filePath=@"d:\超1G文件.zip";
  FileInfo downloadFile = new FileInfo(filePath);//支持1G超大文件
            //等几分钟后删除
            Task task1 = new Task(() =>
            {
                Thread.Sleep(1000 * 60 * 10);
                File.Delete(filePath);
            });
            task1.Start();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = false;
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            var fName = HttpUtility.UrlEncode(Path.GetFileName(filePath)).Replace('+', ' ');//文件名编码
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;   filename=" + fName);
            HttpContext.Current.Response.AppendHeader("Content-Length", downloadFile.Length.ToString());
            HttpContext.Current.Response.WriteFile(downloadFile.FullName);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

posted on 2024-11-28 14:33  anjun_xf  阅读(32)  评论(0)    收藏  举报

导航

TOP