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
parent
commit
b6eb23f936
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs
  2. 21
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController.cs
  3. 21
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController_Tests.cs

2
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.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);

21
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController.cs

@ -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")]

21
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController_Tests.cs

@ -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()

Loading…
Cancel
Save