//下载action
public void GetFile(string guid)
{
if (string.IsNullOrEmpty(guid))
{
Response.Write("<script>alert(‘参数错误‘);</script>");
}
//获取下载文件路径
string filePath = GetDownPath(guid);
if (!System.IO.File.Exists(filePath))
{
Response.Write("<script>alert(‘文件不存在‘);</script>");
return;
}
ChunkTransfer(System.Web.HttpContext.Current, filePath);
HttpContext.Response.Flush();
string tempFolder = fileName.Substring(0, fileName.LastIndexOf(‘\\‘));
//删除临时文件
DeleteDirectory(tempFolder);
}
//guid为创建下载文件时,所保存在的文件夹的名称,且该文件夹下只有一个文件,为要下载的文件
public string GetDownPath(string guid)
{
string fileDirectory = Path.Combine("临时文件夹", "文件路径");
if (Directory.Exists(fileDirectory))
{
DirectoryInfo dirInfo = new DirectoryInfo(fileDirectory);
string fileName = string.Empty;
//然后在当前路径下查找excel文件
FileInfo[] fileInfo = dirInfo.GetFiles("*.xlsx");
if (fileInfo != null && fileInfo.Count() > 0)
{
foreach (FileInfo file in dirInfo.GetFiles("*.xlsx"))
{
fileName = file.FullName.Substring(file.FullName.LastIndexOf("\\") + 1);
}
}
return Path.Combine(fileDirectory, fileName);
}
return "";
}
//将文件放到输出流中
public bool ChunkTransfer(HttpContext httpContext, string filePathName)
{
bool result = false;
if (!System.IO.File.Exists(filePathName))
{
httpContext.Response.StatusCode = 404;
return false;
}
long startPostion = 0;
long endPostion = 0;
string fileName = Path.GetFileName(filePathName);
using (FileStream fileStream = new FileStream(filePathName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (BinaryReader br = new BinaryReader(fileStream))
{
long fileLength = fileStream.Length;
string lastUpdateTime = System.IO.File.GetLastWriteTimeUtc(filePathName).ToString("r");
string eTag = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTime;//恢复下载时提取请求头;
if (httpContext.Request.Headers["If-Range"] != null)
{
if (httpContext.Request.Headers["If-Range"].Replace("\"", "") != eTag)
{//文件修改过
httpContext.Response.StatusCode = 412;//预处理失败
return false;
}
}
httpContext.Response.Clear();
httpContext.Response.Buffer = false;
httpContext.Response.AddHeader("Accept-Ranges", "bytes");
httpContext.Response.AppendHeader("ETag", "\"" + eTag + "\"");
httpContext.Response.AppendHeader("Last-Modified", lastUpdateTime);//把最后修改日期写入响应
httpContext.Response.ContentType = "application/octet-stream";
if (httpContext.Request.UserAgent.IndexOf("MSIE") > -1 || (httpContext.Request.UserAgent.IndexOf("like Gecko") > -1))
{
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20");
}
if (httpContext.Request.UserAgent.ToLower().IndexOf("firefox") > -1)
{
httpContext.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
}
else
{
httpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
}
httpContext.Response.AddHeader("Connection", "Keep-Alive");
httpContext.Response.ContentEncoding = Encoding.UTF8;
if (httpContext.Request.Headers["Range"] != null)//续传
{
httpContext.Response.StatusCode = 206;//续传标识
string[] range = httpContext.Request.Headers["Range"].Split(new char[] { ‘=‘, ‘-‘ });
startPostion = long.Parse(range[1]);//已经下载的字节数
if (startPostion < 0 || startPostion >= fileLength)
{
return false;
}
if (string.IsNullOrEmpty(range[2]))//只指定请求文件起始
{
httpContext.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startPostion, fileLength - 1, fileLength));
httpContext.Response.AddHeader("Content-Length", (fileLength - startPostion).ToString());
}
else//指定请求文件范围
{
endPostion = long.Parse(range[2]);
httpContext.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startPostion, endPostion - startPostion - 1, fileLength));
httpContext.Response.AddHeader("Content-Length", (endPostion - startPostion).ToString());
}
}
else
{//非续传
httpContext.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startPostion, fileLength - 1, fileLength));
httpContext.Response.AddHeader("Content-Length", (fileLength - startPostion).ToString());
}
br.BaseStream.Seek(startPostion, SeekOrigin.Begin);
long maxCount = (long)Math.Ceiling((fileLength - startPostion + 0.0) / TransferBuffer);//分块下载,剩余部分可分成的块数
for (long i = 0; i < maxCount && httpContext.Response.IsClientConnected; i++)
{
httpContext.Response.BinaryWrite(br.ReadBytes(TransferBuffer));
httpContext.Response.Flush();
if (TransferSleep > 0)
{
Thread.Sleep(TransferSleep);
}
}
result = true;
}
}
return result;
}
#region 删除文件夹
/// <summary>
/// 如指定目录存在,删除其并删除该目录中的任何子目录
/// </summary>
/// <param name="path">目录路径</param>
public static void DeleteDirectory(string path)
{
if (string.IsNullOrEmpty(path.Trim()))
return;
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
#endregion