Closed alexmaycon closed 4 years ago
Try using FileStreamResult
like this:
private readonly IWebHostEnvironment _env;
public PersonsController(IWebHostEnvironment env)
{
_env = env;
}
[HttpGet("getfile")]
[AutoWrapIgnore]
public async Task<FileStreamResult> GetFile()
{
var path = $"{_env.ContentRootPath}/Folder/somefile.txt";
var stream = System.IO.File.OpenRead(path);
return new FileStreamResult(stream, "application/octet-stream");
}
Works fine, thanks!
Seems like my issues at #27 similar to this. But in my case, there is no file on my server. I've create my pdf using dinktopdf, then convert its result to MemoryStream
, so it can matchup your suggestion above, but its output still json of my pdf.
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Landscape,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 },
DocumentTitle = "PDF Report",
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = TemplateGenerator.GetAllReports(allReports, idCulture.DateTimeFormat.GetMonthName(Convert.ToInt32(month)), year),
WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/css", "report_style.css") },
FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Right = "Page [page] of [toPage]", Left = "SF-216/2012-AVS Rev.0", Spacing = 1.8 }
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
var file = _converter.Convert(pdf);
var fileStream = new MemoryStream(file);
_logger.LogInfo($"Report PDF Generated. Returned report PDF to client");
return new FileStreamResult(fileStream, "application/pdf");
In a Controller, I have an action that returns the type FileResult. Even adding the [AutoWrapIgnore] tag to the action is returning the error below:
System.ObjectDisposedException: Cannot access a closed Stream. at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc) at AutoWrapper.Base.WrapperBase.InvokeAsyncBase(HttpContext context, AutoWrapperMembers awm) at AutoWrapper.AutoWrapperMiddleware.InvokeAsync(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT
1.ProcessRequestAsync()`Any suggestion?