Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Download Response headers #55

Closed
mansai opened this issue Jun 1, 2020 · 9 comments
Closed

Bug in Download Response headers #55

mansai opened this issue Jun 1, 2020 · 9 comments

Comments

@mansai
Copy link

mansai commented Jun 1, 2020

[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
Copy link
Contributor

hueifeng commented Jun 1, 2020

@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
Copy link
Author

mansai commented Jun 1, 2020

1
2
3

@hueifeng
Copy link
Contributor

hueifeng commented Jun 1, 2020

@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
Copy link
Author

mansai commented Jun 1, 2020

There are still problems
5

@hueifeng
Copy link
Contributor

hueifeng commented Jun 1, 2020

@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
Copy link
Author

mansai commented Jun 1, 2020

DownLoadDemo.zip
6
7

8

@proudmonkey
Copy link
Owner

@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
Copy link
Author

mansai commented Jun 4, 2020

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

@mansai mansai reopened this Jun 4, 2020
@proudmonkey
Copy link
Owner

@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!

@mansai mansai closed this as completed Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants