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

Support filename downloads with special characters #4097

Merged
merged 2 commits into from
Aug 15, 2022

Conversation

knoepdan
Copy link
Contributor

@knoepdan knoepdan commented Aug 2, 2022

FileResponse enhancments
This commit fixes an issue with the fileResponse when the fileName contains special characters (such as Umlaute).

To support special characters in a filename in asp.net, one can use the ContentDisposition.FileNameStar property (instead of FileName). Example in the C# code below:

            var response = new HttpResponseMessage(HttpStatusCode.OK);
            byte[] contents = GetFileResponse(out string filename);
            response.Content = new ByteArrayContent(contents);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

            // fileresponse without support for special characters (e.g.: Umlaute) 
            // response.Content.Headers.ContentDisposition.FileName = filename; // filename="=?utf-8?B?w6TDpMOkMjAyMjAxMDZfMTc0ODUzLkNTVg==?="  (incorrect with special characters)

            // or with support for special characters (e.g. Umlaute)
            response.Content.Headers.ContentDisposition.FileNameStar = filename; // filename*=utf-8''%C3%A4%C3%A4%C3%A420220106_174853.CSV

            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");      

Regarding the regex, please have a look at:


(regex used from a comment of the accepted answer since the accepted answer did have some issues. Tested quite well)

Remark: this PR is related to pull request 3867 which in its first version did not support the case when the server sent a filename with special characters but also a filename without it. This version first uses the filename with special characters and the one without as fallback (same behavior as Mozilla Firefox). See also the discussion in #3867

@RicoSuter RicoSuter merged commit 6a4cc21 into RicoSuter:master Aug 15, 2022
lahma pushed a commit to lahma/NSwag that referenced this pull request Dec 25, 2022
* Support filename downloads with special characters  (related to RicoSuter#3865 and RicoSuter#3867)

* Use spaces

Co-authored-by: Rico Suter <mail@rsuter.com>
lahma pushed a commit to lahma/NSwag that referenced this pull request Jan 20, 2024
* Support filename downloads with special characters  (related to RicoSuter#3865 and RicoSuter#3867)

* Use spaces

Co-authored-by: Rico Suter <mail@rsuter.com>
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

Successfully merging this pull request may close these issues.

None yet

2 participants