Exemple #1
0
        public HttpResult(FileInfo fileResponse, string contentType = null, bool asAttachment = false)
            : this(null, contentType ?? MimeTypes.GetMimeType(fileResponse.Name), HttpStatusCode.OK)
        {
            this.FileInfo              = fileResponse ?? throw new ArgumentNullException(nameof(fileResponse));
            this.LastModified          = fileResponse.LastWriteTime;
            this.AllowsPartialResponse = true;
            if (!FileInfo.Exists)
            {
                throw HttpError.NotFound($"{FileInfo.Name} was not found");
            }

            if (!asAttachment)
            {
                return;
            }

            var headerValue = $"attachment; {HttpExt.GetDispositionFileName(fileResponse.Name)}; size={fileResponse.Length}; " +
                              $"creation-date={fileResponse.CreationTimeUtc.ToString("R").Replace(",", "")}; " +
                              $"modification-date={fileResponse.LastWriteTimeUtc.ToString("R").Replace(",", "")}; " +
                              $"read-date={fileResponse.LastAccessTimeUtc.ToString("R").Replace(",", "")}";

            this.Headers = new Dictionary <string, string>
            {
                { HttpHeaders.ContentDisposition, headerValue },
            };
        }
Exemple #2
0
        public HttpResult(IVirtualFile fileResponse, string contentType = null, bool asAttachment = false)
            : this(null, contentType ?? MimeTypes.GetMimeType(fileResponse.Name), HttpStatusCode.OK)
        {
            if (fileResponse == null)
            {
                throw new ArgumentNullException(nameof(fileResponse));
            }

            this.AllowsPartialResponse = true;
            this.LastModified          = fileResponse.LastModified;
            this.VirtualFile           = fileResponse;
            this.ResponseStream        = fileResponse.OpenRead();

            if (!asAttachment)
            {
                return;
            }

            var headerValue = $"attachment; {HttpExt.GetDispositionFileName(fileResponse.Name)}; size={fileResponse.Length}; modification-date={fileResponse.LastModified.ToString("R").Replace(",", "")}";

            this.Headers = new Dictionary <string, string>
            {
                { HttpHeaders.ContentDisposition, headerValue },
            };
        }