Search code examples
c#asp.net-mvcpdfdata-uri

convert datauri pdf to file for attaching to email


I am using a basic telerik export document to pdf function. this works great to export the page directly to the user. I then pass this to a controller as a string via datauri.

how can I convert it back to a file so that I can attach it to an email?

imageData: "data:application/pdf;base64,JVBERi0xLjQKJcLB2s/OCgoxIDAg...

Solution

  • I found a way to do this. replace a bit of the header, convert to byes, make a new stream, attach stream to email.

     imageData = imageData.Replace("data:application/pdf;base64,", "");
     byte[] bytes = Convert.FromBase64String(imageData);
    
     Stream stream = new MemoryStream(bytes);
     email.Attachments.AddFileAttachment("SALOR.pdf",stream);