Пример #1
0
        /// <summary>
        ///     Sends file as response and requests the data to be downloaded as an attachment
        /// </summary>
        /// <param name="filePath">The local path of the file to send</param>
        /// <param name="fileName">The name filename the client receives the file with, defaults to using the actual filename</param>
        /// <param name="contentType">
        ///     The mime type for the file, when set to null, the system will try to detect based on file
        ///     extension
        /// </param>
        /// <param name="status">The status code for the response</param>
        public async Task Download(string filePath, string fileName = null, string contentType = "",
                                   HttpStatusCode status            = HttpStatusCode.OK)
        {
            UnderlyingResponse.StatusCode  = (int)status;
            UnderlyingResponse.ContentType = GetMime(contentType, filePath);
            var name = string.IsNullOrEmpty(fileName) ? Path.GetFileName(filePath) : fileName;

            AddHeader("Content-disposition", $"attachment; filename=\"{name}\"");
            await UnderlyingResponse.SendFileAsync(filePath);

            Closed = true;
        }
Пример #2
0
        /// <summary>
        ///     Sends file as response and requests the data to be displayed in-browser if possible
        /// </summary>
        /// <param name="filePath">The local path of the file to send</param>
        /// <param name="contentType">
        ///     The mime type for the file, when set to null, the system will try to detect based on file
        ///     extension
        /// </param>
        /// <param name="status">The status code for the response</param>
        public async Task SendFile(string filePath, string contentType = null, HttpStatusCode status = HttpStatusCode.OK)
        {
            UnderlyingResponse.StatusCode = (int)status;
            if (contentType == null && !MimeTypes.TryGetValue(Path.GetExtension(filePath), out contentType))
            {
                contentType = "application/octet-stream";
            }
            UnderlyingResponse.ContentType = contentType;
            UnderlyingResponse.Headers.Add("Accept-Ranges", "bytes");
            UnderlyingResponse.Headers.Add("Content-disposition", $"inline; filename=\"{Path.GetFileName(filePath)}\"");
            await UnderlyingResponse.SendFileAsync(filePath);

            Closed = true;
        }
Пример #3
0
        /// <summary>
        ///     Sends file as response and requests the data to be downloaded as an attachment
        /// </summary>
        /// <param name="filePath">The local path of the file to send</param>
        /// <param name="fileName">The name filename the client receives the file with, defaults to using the actual filename</param>
        /// <param name="contentType">
        ///     The mime type for the file, when set to null, the system will try to detect based on file
        ///     extension
        /// </param>
        /// <param name="status">The status code for the response</param>
        public async Task Download(string filePath, string fileName = null, string contentType = "",
                                   HttpStatusCode status            = HttpStatusCode.OK)
        {
            UnderlyingResponse.StatusCode = (int)status;
            if (contentType == null && !MimeTypes.TryGetValue(Path.GetExtension(filePath), out contentType))
            {
                contentType = "application/octet-stream";
            }
            UnderlyingResponse.ContentType = contentType;
            var name = string.IsNullOrEmpty(fileName) ? Path.GetFileName(filePath) : fileName;

            UnderlyingResponse.Headers.Add("Content-disposition", $"attachment; filename=\"{name}\"");
            await UnderlyingResponse.SendFileAsync(filePath);

            Closed = true;
        }
Пример #4
0
        /// <summary>
        ///     Sends file as response and requests the data to be displayed in-browser if possible
        /// </summary>
        /// <param name="filePath">The local path of the file to send</param>
        /// <param name="contentType">
        ///     The mime type for the file, when set to null, the system will try to detect based on file
        ///     extension
        /// </param>
        /// <param name="handleRanges">Whether to enable handling of range-requests for the file(s) served</param>
        /// <param name="status">The status code for the response</param>
        public async Task SendFile(string filePath, string contentType = null, bool handleRanges = true,
                                   HttpStatusCode status = HttpStatusCode.OK)
        {
            if (handleRanges)
            {
                AddHeader("Accept-Ranges", "bytes");
            }

            var fileSize = new FileInfo(filePath).Length;
            var range    = UnderlyingContext.Request.GetTypedHeaders().Range;

            if (range != null && range.Ranges.Any())
            {
                var firstRange = range.Ranges.First();
                if (range.Unit != "bytes" || (!firstRange.From.HasValue && !firstRange.To.HasValue))
                {
                    await SendStatus(HttpStatusCode.BadRequest);

                    return;
                }

                var offset = firstRange.From ?? fileSize - firstRange.To.Value;
                var length = firstRange.To.HasValue
                    ? fileSize - offset - (fileSize - firstRange.To.Value)
                    : fileSize - offset;

                UnderlyingResponse.StatusCode    = (int)HttpStatusCode.PartialContent;
                UnderlyingResponse.ContentType   = GetMime(contentType, filePath);
                UnderlyingResponse.ContentLength = length;
                AddHeader("Content-Disposition", $"inline; filename=\"{Path.GetFileName(filePath)}\"");
                AddHeader("Content-Range", $"bytes {offset}-{offset + length - 1}/{fileSize}");
                await UnderlyingResponse.SendFileAsync(filePath, offset, length);
            }
            else
            {
                UnderlyingResponse.StatusCode    = (int)status;
                UnderlyingResponse.ContentType   = GetMime(contentType, filePath);
                UnderlyingResponse.ContentLength = fileSize;
                AddHeader("Content-Disposition", $"inline; filename=\"{Path.GetFileName(filePath)}\"");
                await UnderlyingResponse.SendFileAsync(filePath);
            }

            Closed = true;
        }