44 using System . Text . Json ;
55 using System . Threading ;
66 using Contracts ;
7+ using Microsoft . AspNetCore . Http ;
78 using Microsoft . AspNetCore . Mvc ;
9+ using Microsoft . Net . Http . Headers ;
810 using Particular . LicensingComponent . Report ;
911
1012 [ ApiController ]
@@ -40,12 +42,14 @@ public async Task<ReportGenerationState> CanThroughputReportBeGenerated(Cancella
4042
4143 [ Route ( "report/file" ) ]
4244 [ HttpGet ]
43- public async Task < IActionResult > GetThroughputReportFile ( [ FromQuery ( Name = "spVersion" ) ] string ? spVersion , CancellationToken cancellationToken )
45+ public async Task GetThroughputReportFile ( [ FromQuery ( Name = "spVersion" ) ] string ? spVersion , CancellationToken cancellationToken )
4446 {
4547 var reportStatus = await CanThroughputReportBeGenerated ( cancellationToken ) ;
4648 if ( ! reportStatus . ReportCanBeGenerated )
4749 {
48- return BadRequest ( $ "Report cannot be generated - { reportStatus . Reason } ") ;
50+ HttpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
51+ await HttpContext . Response . WriteAsync ( $ "Report cannot be generated – { reportStatus . Reason } ", cancellationToken ) ;
52+ return ;
4953 }
5054
5155 var report = await throughputCollector . GenerateThroughputReport (
@@ -55,16 +59,16 @@ public async Task<IActionResult> GetThroughputReportFile([FromQuery(Name = "spVe
5559
5660 var fileName = $ "{ report . ReportData . CustomerName } .throughput-report-{ report . ReportData . EndTime : yyyyMMdd-HHmmss} ";
5761
58- using var memoryStream = new MemoryStream ( ) ;
59- using ( var archive = new ZipArchive ( memoryStream , ZipArchiveMode . Create , true ) )
62+ HttpContext . Response . ContentType = "application/zip" ;
63+ HttpContext . Response . Headers [ HeaderNames . ContentDisposition ] = new ContentDispositionHeaderValue ( "attachment" )
6064 {
61- var entry = archive . CreateEntry ( $ "{ fileName } .json") ;
62- await using var entryStream = entry . Open ( ) ;
63- await JsonSerializer . SerializeAsync ( entryStream , report , SerializationOptions . IndentedWithNoEscaping , cancellationToken ) ;
64- }
65+ FileName = fileName
66+ } . ToString ( ) ;
6567
66- memoryStream . Position = 0 ;
67- return File ( memoryStream , "application/zip" , fileDownloadName : $ "{ fileName } .zip") ;
68+ using var archive = new ZipArchive ( HttpContext . Response . Body , ZipArchiveMode . Create , leaveOpen : true ) ;
69+ var entry = archive . CreateEntry ( $ "{ Path . GetFileNameWithoutExtension ( fileName ) } .json") ;
70+ await using var entryStream = entry . Open ( ) ;
71+ await JsonSerializer . SerializeAsync ( entryStream , report , SerializationOptions . IndentedWithNoEscaping , cancellationToken ) ;
6872 }
6973
7074 [ Route ( "settings/info" ) ]
0 commit comments