微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何以正确的方式将字节数组转换为FormFile?

如何解决如何以正确的方式将字节数组转换为FormFile?

此示例从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,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();
                }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。