0

此示例从 html 创建 pdf 字节数组。我想在电子邮件附件中添加一些文件。如何以正确的方式将字节数组转换为 FormFile?
这是我的代码:

public async Task<byte[]> ShowPdfByInstructionId(int id)
            {
                InstructionTemplateDTO template = new InstructionTemplateDTO();
                template = GetInstructionTemplate(id);
                
                var actionPdf = new Rotativa.AspNetCore.ViewAsPdf("InstructionPdf",   template ) 
                { 
                    FileName = template.number + ".pdf",
                    PageSize = Rotativa.AspNetCore.Options.Size.A4,
                    PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
                    PageHeight = 20,
                };
                var pdfData = await actionPdf.BuildFile(this.ControllerContext);
                return pdfData;
            }
    
    private async Task<ActionResult> SendMail( instructions instruction)
            {
                MailRequest mailRequest = new MailRequest();
                try 
                    { 
                            mailRequest.ToEmail = partner_contact.mail;
                            mailRequest.Body = "This is a body of message";
                            var pdfData = ShowPdfByInstructionId(instruction.id).Result;
                            using (var ms = new MemoryStream(pdfData))
                            {
                                ms.Seek(0, SeekOrigin.Begin);
         var formFile = new FormFile(ms, 0, ms.Length, null, instruction.id.ToString() + ".pdf")
                                {
                                    Headers = new HeaderDictionary(),
                                    ContentType = "application/pdf"
                                };
                                mailRequest.Attachments.Add(formFile);//System.NullReferenceException: "Object reference not set to an instance of an object."-MailRequest.Attachments.get return null.
                            await _publishEndpoint.Publish<MailRequest>(mailRequest);

                    }
                    return Ok();
                }
4

0 回答 0