Browse Source
Merge pull request #14365 from abpframework/liangshiwei/patch-1
Fix download file Chinese name garbled problem
pull/14373/head
maliming
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
43 additions and
1 deletions
-
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController_Tests.cs
|
|
|
@ -27,7 +27,7 @@ public class RemoteStreamContentOutputFormatter : OutputFormatter |
|
|
|
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); |
|
|
|
|
|
|
|
@ -19,6 +19,27 @@ public class RemoteStreamContentTestController : AbpController |
|
|
|
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")] |
|
|
|
|
|
|
|
@ -3,6 +3,7 @@ using System.Net.Http; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Web; |
|
|
|
using Shouldly; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
@ -19,6 +20,26 @@ public class RemoteStreamContentTestController_Tests : AspNetCoreMvcTestBase |
|
|
|
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() |
|
|
|
|