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

Fix download file Chinese name garbled problem #14365

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
context.HttpContext.Response.ContentType = remoteStream.ContentType;
context.HttpContext.Response.ContentLength = remoteStream.ContentLength;

if (!remoteStream.FileName.IsNullOrWhiteSpace())
if (!remoteStream.FileName.IsNullOrWhiteSpace() && !context.HttpContext.Response.Headers.ContainsKey(HeaderNames.ContentDisposition))
{
var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName(remoteStream.FileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ public async Task<IRemoteStreamContent> DownloadAsync()
memoryStream.Position = 0;
return new RemoteStreamContent(memoryStream, "download.rtf", "application/rtf");
}

[HttpGet]
[Route("Download-With-Custom-Content-Disposition")]
public async Task<IRemoteStreamContent> Download_With_Custom_Content_Disposition_Async()
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("DownloadAsync"));
memoryStream.Position = 0;
Response.Headers.Add("Content-Disposition", "attachment; filename=myDownload.rtf");
return new RemoteStreamContent(memoryStream, "download.rtf", "application/rtf");
}

[HttpGet]
[Route("Download_With_Chinese_File_Name")]
public async Task<IRemoteStreamContent> Download_With_Chinese_File_Name_Async()
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("DownloadAsync"));
memoryStream.Position = 0;
return new RemoteStreamContent(memoryStream, "下载文件.rtf", "application/rtf");
}

[HttpPost]
[Route("Upload")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Shouldly;
using Xunit;

Expand All @@ -19,6 +20,26 @@ public async Task DownloadAsync()
result.Content.Headers.ContentLength.ShouldBe("DownloadAsync".Length);
(await result.Content.ReadAsStringAsync()).ShouldBe("DownloadAsync");
}

[Fact]
public async Task Download_With_Custom_Content_Disposition_Async()
{
var result = await GetResponseAsync("/api/remote-stream-content-test/download-with-custom-content-disposition");
result.Content.Headers.ContentType?.ToString().ShouldBe("application/rtf");
result.Content.Headers.ContentDisposition?.FileName.ShouldBe("myDownload.rtf");
result.Content.Headers.ContentLength.ShouldBe("DownloadAsync".Length);
(await result.Content.ReadAsStringAsync()).ShouldBe("DownloadAsync");
}

[Fact]
public async Task Download_With_Chinese_File_Name_Async()
{
var result = await GetResponseAsync("/api/remote-stream-content-test/download_with_chinese_file_name");
result.Content.Headers.ContentType?.ToString().ShouldBe("application/rtf");
result.Content.Headers.ContentDisposition?.FileNameStar.ShouldBe("下载文件.rtf");
result.Content.Headers.ContentLength.ShouldBe("DownloadAsync".Length);
(await result.Content.ReadAsStringAsync()).ShouldBe("DownloadAsync");
}

[Fact]
public async Task UploadAsync()
Expand Down