Asp.Net 分段,断点,下载
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
static
void
DownloadFile(HttpContext context,
string
filePath)
{
UtilLite.Guard < ArgumentNullException > (context == null );
FileInfo file = new FileInfo(filePath);
UtilLite.Guard < FileNotFoundException > ( ! file.Exists);
context.Response.Clear();
context.Response.Buffer = false ;
context.Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(file.Name)));
context.Response.AppendHeader( " Content-Length " , file.Length.ToString());
context.Response.ContentType = " application/octet-stream " ;
context.Response.WriteFile(filePath);
context.Response.Flush();
context.Response.End();
}
public static void Range(HttpContext context, string filePath)
{
UtilLite.Guard < ArgumentNullException > (context == null );
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.Clear();
Response.Buffer = false ;
Response.AddHeader( " Accept-Ranges " , " bytes " );
int bufferSize = 4096 ;
byte [] buffer = new byte [bufferSize];
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
{
long p = 0L , l = stream.Length;
#region Resolve Range
string range = Request.Headers[ " Range " ];
if (range != null )
{
// Range:bytes=1024-
// Range:bytes=1024-2048
// Range:bytes=-1024
// Range:bytes=0-512,1024-2048
int index = range.IndexOf( " = " );
if (index != - 1 )
{
string [] ranges = range.Substring(index + 1 ).Split( ' , ' );
if (ranges.Length > 1 )
{
// not supported multipart/byterange
}
else
{
bool flag = false ;
if (ranges[ 0 ].StartsWith( " - " ))
{
long _p, _absp;
if ( long .TryParse(ranges[ 0 ], out _p) && (_absp = Math.Abs(_p)) <= l)
{
if (_p < 0 )
{
p = l - _absp;
l = _absp;
flag = true ;
}
}
}
else
{
ranges = ranges[ 0 ].Split( ' - ' );
if (ranges.Length == 2 )
{
long _p, _l;
if (ranges[ 1 ] == string .Empty)
{
if ( long .TryParse(ranges[ 0 ], out _p) && _p <= l)
{
p = _p;
flag = true ;
}
}
else if ( long .TryParse(ranges[ 0 ], out _p) && long .TryParse(ranges[ 1 ], out _l) && _p > 0 && _l > 0 && _p < _l && _l < l)
{
p = _p;
l = _l + 1 ;
flag = true ;
}
}
}
if (flag)
{
Response.StatusCode = 206 ;
Response.AddHeader( " Content-Range " , " bytes " + p.ToString() + " - " + l.ToString() + " / " + stream.Length.ToString());
}
else
{
Response.StatusCode = 416 ; // Requested range not satisfiable
}
}
}
}
#endregion
Response.AddHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(Path.GetFileName(filePath))));
Response.AddHeader( " Content-Length " , l.ToString());
Response.ContentType = " application/octet-stream " ;
stream.Position = p;
int read;
while (Response.IsClientConnected && p < l)
{
Response.OutputStream.Write(buffer, 0 , read = stream.Read(buffer, 0 , bufferSize));
Response.Flush();
p += read;
}
}
Response.End();
}
{
UtilLite.Guard < ArgumentNullException > (context == null );
FileInfo file = new FileInfo(filePath);
UtilLite.Guard < FileNotFoundException > ( ! file.Exists);
context.Response.Clear();
context.Response.Buffer = false ;
context.Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(file.Name)));
context.Response.AppendHeader( " Content-Length " , file.Length.ToString());
context.Response.ContentType = " application/octet-stream " ;
context.Response.WriteFile(filePath);
context.Response.Flush();
context.Response.End();
}
public static void Range(HttpContext context, string filePath)
{
UtilLite.Guard < ArgumentNullException > (context == null );
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.Clear();
Response.Buffer = false ;
Response.AddHeader( " Accept-Ranges " , " bytes " );
int bufferSize = 4096 ;
byte [] buffer = new byte [bufferSize];
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
{
long p = 0L , l = stream.Length;
#region Resolve Range
string range = Request.Headers[ " Range " ];
if (range != null )
{
// Range:bytes=1024-
// Range:bytes=1024-2048
// Range:bytes=-1024
// Range:bytes=0-512,1024-2048
int index = range.IndexOf( " = " );
if (index != - 1 )
{
string [] ranges = range.Substring(index + 1 ).Split( ' , ' );
if (ranges.Length > 1 )
{
// not supported multipart/byterange
}
else
{
bool flag = false ;
if (ranges[ 0 ].StartsWith( " - " ))
{
long _p, _absp;
if ( long .TryParse(ranges[ 0 ], out _p) && (_absp = Math.Abs(_p)) <= l)
{
if (_p < 0 )
{
p = l - _absp;
l = _absp;
flag = true ;
}
}
}
else
{
ranges = ranges[ 0 ].Split( ' - ' );
if (ranges.Length == 2 )
{
long _p, _l;
if (ranges[ 1 ] == string .Empty)
{
if ( long .TryParse(ranges[ 0 ], out _p) && _p <= l)
{
p = _p;
flag = true ;
}
}
else if ( long .TryParse(ranges[ 0 ], out _p) && long .TryParse(ranges[ 1 ], out _l) && _p > 0 && _l > 0 && _p < _l && _l < l)
{
p = _p;
l = _l + 1 ;
flag = true ;
}
}
}
if (flag)
{
Response.StatusCode = 206 ;
Response.AddHeader( " Content-Range " , " bytes " + p.ToString() + " - " + l.ToString() + " / " + stream.Length.ToString());
}
else
{
Response.StatusCode = 416 ; // Requested range not satisfiable
}
}
}
}
#endregion
Response.AddHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(Path.GetFileName(filePath))));
Response.AddHeader( " Content-Length " , l.ToString());
Response.ContentType = " application/octet-stream " ;
stream.Position = p;
int read;
while (Response.IsClientConnected && p < l)
{
Response.OutputStream.Write(buffer, 0 , read = stream.Read(buffer, 0 , bufferSize));
Response.Flush();
p += read;
}
}
Response.End();
}