Loading byte [] of generated PDF using nodeervices in Asp.net Core 1.1

I am trying to load a pdf file created by nodeServices, which is in the form of an array of bytes. here is my original code:

[HttpGet]
[Route("[action]/{appId}")]
public async Task<IActionResult> Pdf(Guid appId, [FromServices] INodeServices nodeServices)
{
    // generateHtml(appId) is a function where my model is converted to html.
    // then nodeservices will generate the pdf for me as byte[].
    var result = await nodeServices.InvokeAsync<byte[]>("./pdf", 
            await generateHtml(appId));
    HttpContext.Response.ContentType = "application/pdf";
    HttpContext.Response.Headers.Add("x-filename", "myFile.pdf");
    HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "x-filename");
    HttpContext.Response.Body.Write(result, 0, result.Length);
    return new ContentResult();
}

This code worked fine, it will show the pdf file in the browser, for example. chrome, and when I try to download it, I get a "crash, network error".

I searched here and there, I saw several suggestions for returning a file:

return File(result, "application/pdf");

which didn't work either, adding a Content-Disposition header:

HttpContext.Response.Headers.Add("Content-Disposition", string.Format("inline;filename={0}", "myFile.pdf"));

FileStreamResult, . , (byte []) , , , , , , , :

var result = await nodeServices.InvokeAsync<byte[]>("./pdf", await generateHtml(appId));
var tempfilepath = Path.Combine(_environment.WebRootPath, $"temp/{appId}.pdf");

System.IO.File.WriteAllBytes(tempfilepath, result);

var memory = new MemoryStream();
using (var stream = new FileStream(tempfilepath, FileMode.Open))
{
    await stream.CopyToAsync(memory);
}
memory.Position = 0;

return File(memory, "application/pdf", Path.GetFileName(tempfilepath));

! , , , , : ?

+6
1

FileContentResult . File(), fileContents , contentType - .

, - :

public async Task<IActionResult> Pdf(Guid appId, [FromServices] INodeServices nodeServices)
{
    var result = await nodeServices.InvokeAsync<byte[]>("./pdf", 
            await generateHtml(appId));

    return File(result, "application/pdf","myFile.pdf");
}
+3

All Articles