/// <summary> /// 下载文件 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public HttpResponseMessage GetFile(string fileName) { if (!FileProvider.Exists(fileName)) { throw new HttpResponseException(HttpStatusCode.NotFound); } long fileLength = FileProvider.GetLength(fileName); var fileInfo = GetFileInfoFromRequest(this.Request, fileLength); //获取剩余部分文件流 var stream = new PartialContentFileStream(FileProvider.Open(fileName), fileInfo.From, fileInfo.To); var response = new HttpResponseMessage(); response.Content = new StreamContent(stream, BufferSize); SetResponseHeaders(response, fileInfo, fileLength, fileName); return(response); }
public IActionResult GetFile() { string fileName = @"D:\Files\BI_V2.0.zip"; if (!FileUtil.IsExistFile(fileName)) { return(new StatusCodeResult(StatusCodes.Status404NotFound)); } //获取下载文件长度 var fileLength = FileUtil.GetLength(fileName); //初始化下载文件信息 var fileInfo = _downloadServices.GetFileInfoFromRequest(_contextAccessor.HttpContext.Request, fileLength); //获取剩余部分文件流 var stream = new PartialContentFileStream(FileUtil.GetStreamFrom(fileName), fileInfo.From, fileInfo.To); var contentType = MimeMappingUtil.GetMimeMapping(fileName); //设置响应 请求头 _downloadServices.SetResponseHeaders(_contextAccessor.HttpContext.Response, fileInfo, contentType, fileLength, fileName); return(new FileStreamResult(stream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(contentType))); }
public HttpResponseMessage GetFileResponse(string filePath) { var bytes = HttpServerUtility.UrlTokenDecode(filePath); var path = HttpUtility.UrlDecode(bytes, Encoding.UTF8); if (string.IsNullOrEmpty(path) || !File.Exists(path)) { throw new HttpResponseException(HttpStatusCode.NotFound); } var fileName = Path.GetFileName(path); var fielStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); long fileLength = fielStream.Length; var fileInfo = GetFileByRequest(Request, fileLength); //获取剩余部分文件流 var stream = new PartialContentFileStream(fielStream, fileInfo.From, fileInfo.To); var response = new HttpResponseMessage { Content = new StreamContent(stream, BufferSize) }; SetResponseHeaders(response, fileInfo, fileLength, fileName); return(response); }
public IActionResult GetFile(string fileName) { fileName = "cn_windows_8_1_x64_dvd_2707237.iso"; if (!_fileProvider.Exists(fileName)) { return(new StatusCodeResult(StatusCodes.Status404NotFound)); } //获取下载文件长度 var fileLength = _fileProvider.GetLength(fileName); //初始化下载文件信息 var fileInfo = GetFileInfoFromRequest(_context.Request, fileLength); //获取剩余部分文件流 var stream = new PartialContentFileStream(_fileProvider.Open(fileName), fileInfo.From, fileInfo.To); //设置响应 请求头 SetResponseHeaders(_context.Response, fileInfo, fileLength, fileName); return(new FileStreamResult(stream, new MediaTypeHeaderValue(MimeType))); }