/// <summary>
/// 输出硬盘文件,提供下载
/// </summary>
/// <param name="context">HttpContext对象</param>
/// <param name="_fullPath">要下载的文件的完整路径</param>
/// <returns>是否成功</returns>
public static bool ResponseFile(HttpContext context, string _fullPath)
{
HttpRequest _Request = context.Request;
HttpResponse _Response = context.Response;
string strFileName = new FileInfo(_fullPath).Name;
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack =10240 ; //10K bytes 进行拆包,每包大小
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(Convert.ToDouble( (fileLength - startBytes) / pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
_Response.Flush();
}
else
{
i = maxCount;
SqlHelp.SaveLog.WriteLog("用户取消下载!");
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch( Exception ex)
{
return false;
}
return true;
}
/// 输出硬盘文件,提供下载
/// </summary>
/// <param name="context">HttpContext对象</param>
/// <param name="_fullPath">要下载的文件的完整路径</param>
/// <returns>是否成功</returns>
public static bool ResponseFile(HttpContext context, string _fullPath)
{
HttpRequest _Request = context.Request;
HttpResponse _Response = context.Response;
string strFileName = new FileInfo(_fullPath).Name;
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack =10240 ; //10K bytes 进行拆包,每包大小
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(Convert.ToDouble( (fileLength - startBytes) / pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
_Response.Flush();
}
else
{
i = maxCount;
SqlHelp.SaveLog.WriteLog("用户取消下载!");
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch( Exception ex)
{
return false;
}
return true;
}

浙公网安备 33010602011771号