/// <summary>
 /// 设置响应头信息
 /// </summary>
 /// <param name="response"></param>
 /// <param name="fileInfo"></param>
 /// <param name="fileLength"></param>
 /// <param name="fileName"></param>
 private void SetResponseHeaders(HttpResponseMessage response, CustomFileInfo.FileInfo fileInfo,
                                 long fileLength, string fileName)
 {
     response.Headers.AcceptRanges.Add("bytes");
     response.StatusCode = fileInfo.IsPartial ? HttpStatusCode.PartialContent
         : HttpStatusCode.OK;
     response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
     response.Content.Headers.ContentDisposition.FileName = fileName;
     response.Content.Headers.ContentType   = new MediaTypeHeaderValue("application/octet-stream");
     response.Content.Headers.ContentLength = fileInfo.Length;
     if (fileInfo.IsPartial)
     {
         response.Content.Headers.ContentRange
             = new ContentRangeHeaderValue(fileInfo.From, fileInfo.To, fileLength);
     }
 }
        private CustomFileInfo.FileInfo GetFileInfoFromRequest(HttpRequestMessage request, long entityLength)
        {
            var response = new CustomFileInfo.FileInfo
            {
                From      = 0,
                To        = entityLength - 1,
                IsPartial = false,
                Length    = entityLength
            };
            var rangeHeader = request.Headers.Range;

            if (rangeHeader != null && rangeHeader.Ranges.Count != 0)
            {
                if (rangeHeader.Ranges.Count > 1)
                {
                    throw new HttpResponseException(HttpStatusCode.RequestedRangeNotSatisfiable);
                }
                var range = rangeHeader.Ranges.First();
                if (range.From.HasValue && range.From < 0 || range.To.HasValue && range.To > entityLength - 1)
                {
                    throw new HttpResponseException(HttpStatusCode.RequestedRangeNotSatisfiable);
                }

                response.From      = range.From ?? 0;
                response.To        = range.To ?? entityLength - 1;
                response.IsPartial = true;
                response.Length    = entityLength;
                if (range.From.HasValue && range.To.HasValue)
                {
                    response.Length = range.To.Value - range.From.Value + 1;
                }
                else if (range.From.HasValue)
                {
                    response.Length = entityLength - range.From.Value + 1;
                }
                else if (range.To.HasValue)
                {
                    response.Length = range.To.Value + 1;
                }
            }

            return(response);
        }