proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
679 stars 82 forks source link

Bug in Download Response headers #55

Closed mansai closed 4 years ago

mansai commented 4 years ago

[HttpPost]
[AutoWrapIgnore] public async Task< FileStreamResult> GetExcel() { var filePath = "xxx..xlsx"; var file = System.IO.File.OpenRead(filePath); var provider = new FileExtensionContentTypeProvider();
var fileInfo = new System.IO.FileInfo(filePath);
var memi = provider.Mappings[fileInfo.Extension]; return File(file, memi, fileInfo.Name); } Bug: content-type: application/json {978E9BF7-6A16-49EE-938F-0BB2E03FEDA6}_20200601115104

Right: content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

hueifeng commented 4 years ago

@mansai I think it should be the following

     [HttpGet]
        [AutoWrapIgnore]
        public async Task<FileStreamResult> GetExcel()
        {
            var filePath = "test.txt";
            var file = System.IO.File.OpenRead(filePath);
            var provider = new FileExtensionContentTypeProvider();
            var fileInfo = new System.IO.FileInfo(filePath);
            var memi = provider.Mappings[fileInfo.Extension];
            return File(file, memi, fileInfo.Name);
        }
mansai commented 4 years ago

1 2 3

hueifeng commented 4 years ago

@mansai Ok, I have reproduced the problem, you can first use the following code snippet to return your expected result.

     app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions
            {
                IsApiOnly = false,
            });
mansai commented 4 years ago

There are still problems 5

hueifeng commented 4 years ago

@mansai I'm sorry I can't reproduce your results. I do n’t know if you have executed dotnet build, or to better solve the problem, I suggest you provide a reproducible demo

mansai commented 4 years ago

DownLoadDemo.zip 6 7

8

proudmonkey commented 4 years ago

@mansai Thanks for the feedback! I appreciate it. This should now be resolved with the latest release here: https://www.nuget.org/packages/AutoWrapper.Core/4.2.0

Sample usage:

        [HttpGet("getfile")]
        [AutoWrapIgnore]
        public FileStreamResult GetFile2()
        {
            var filePath = $"{Environment.CurrentDirectory}/Logs/log_20200501.txt";
            var file = System.IO.File.OpenRead(filePath);
            var provider = new FileExtensionContentTypeProvider();
            var fileInfo = new System.IO.FileInfo(filePath);
            var memi = provider.Mappings[fileInfo.Extension];
            return File(file, memi, fileInfo.Name);
        }

        [HttpGet("getfile2")]
        [AutoWrapIgnore]
        public FileStreamResult GetFile()
        {
            var path = $"{Environment.CurrentDirectory}/Logs/log_20200501.txt";
            var stream = System.IO.File.OpenRead(path);
            return new FileStreamResult(stream, "application/octet-stream");
        }

        [HttpGet("getpdf")]
        [AutoWrapIgnore]
        public IActionResult GetPdf()
        {
            var path = $"{Environment.CurrentDirectory}/sample.pdf";
            byte[] fileBytes = System.IO.File.ReadAllBytes(path);
            return new FileContentResult(fileBytes, "application/pdf");
        }

        [HttpGet("getpdf2")]
        [AutoWrapIgnore]
        public IActionResult GetPdf2()
        {
            var path = $"{Environment.CurrentDirectory}/sample.pdf";
            var stream = new FileStream(path, FileMode.Open);
            return new FileStreamResult(stream, "application/pdf");
        }

        //returns text/plain
        [HttpGet("getstring")]
        [AutoWrapIgnore]
        public ContentResult GetString()
        {
            return Content("Hello");
        }
mansai commented 4 years ago

txt file download ok,Unable to open Excel or pdf,content-length changed

proudmonkey commented 4 years ago

@mansai

I've released v4.2.2 here: https://www.nuget.org/packages/AutoWrapper.Core/4.2.2

Please update to that and it should fix the issue.

Thanks!