Wenn ich das Dokument in der Debug -Datei speichere, funktioniert es einwandfrei, aber ich möchte, dass der Client das Dokument auf ihrem Computer herunterladen und speichern kann.
Code: Select all
public async Task GenerateDocuementAsync(
string annexType,
IEnumerable entities)
{
_templatePaths.TryGetValue(annexType, out var templatePath);
if (templatePath == null)
{
throw new ArgumentException("Annex type not found");
}
var fileBytes = await File.ReadAllBytesAsync(templatePath);
var tempStream = new MemoryStream();
await tempStream.WriteAsync(fileBytes);
tempStream.Position = 0;
byte[] teste;
using (var doc = WordprocessingDocument.Open(tempStream, true))
{
var body = doc.MainDocumentPart.Document.Body;
TableHelper.ReplaceTablePlaceholder(body, entities);
doc.MainDocumentPart.Document.Save();
tempStream.Position = 0;
teste = tempStream.ToArray();
}
Console.WriteLine($"length tempStream: {tempStream.Length}");
tempStream.Position = 0;
return teste;
}
Code: Select all
[HttpPost]
public async Task GenerateContract([FromBody] ContractGenerationModel request)
{
try
{
var contractEntities = _mapper.Map(request.Entities);
var wordDocument = await _contractGeneratorService.GenerateContractAsync(request.AnnexType, contractEntities);
Console.WriteLine($"wordDocument length: {wordDocument.Length}");
return File(wordDocument, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "generated.docx");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return StatusCode(500, "An error occurred while generating the contract.");
}
}