如何使用asp.net c将使用jsPDF生成的PDF附加到邮件中
我需要知道是否有任何方法可以附加使用jsPDF生成的PDF文件并将其邮寄到asp.net C#中?
我在c#中有以下代码
MailMessage message = new MailMessage(fromAddress, toAddress); message.Subject = subject; message.IsBodyHtml = true; message.Body = StrContent.ToString(); //message.Attachments.Add(new Attachment("getDPF()")); smtp.Send(message);
我正在使用JsPDF库,如下所示:
function getPDF() { var doc = new jsPDF(); doc.text(20, 20, 'TEST Message'); doc.addPage(); //doc.save('volt.pdf'); }
在发送之前有没有办法将它附在邮件中? 提前致谢。
您无法从服务器代码(c#)调用客户端代码(Javascript函数)。 您只能通过(HTTP / HTTPs)协议进行通信。
我认为您需要从客户端生成PDF,然后将该PDF发送到服务器,以便您可以将PDF附加到电子邮件中。
在这种情况下,您需要首先生成PDF并将其作为base64字符串发送到服务器。
然后,您可以将base64字符串转换为C#中的PDF并将其作为附件邮寄。
客户端:
function generatePdf() { var doc = new jsPdf(); doc.text("jsPDF to Mail", 40, 30); var binary = doc.output(); return binary ? btoa(binary) : ""; }
将base64 pdf内容发布到服务器:
var reqData = generatePdf(); $.ajax({ url:url, data: JSON.stringify({data:reqData}), dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success:function(){} });
在服务器上(MVC控制器):
public ActionResult YourMethod(string data) { //create pdf var pdfBinary = Convert.FromBase64String(data); var dir = Server.MapPath("~/DataDump"); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); var fileName = dir + "\PDFnMail-" + DateTime.Now.ToString("yyyyMMdd-HHMMss") + ".pdf"; // write content to the pdf using (var fs = new FileStream(fileName, FileMode.Create)) using (var writer = new BinaryWriter(fs)) { writer.Write(pdfBinary, 0, pdfBinary.Length); writer.Close(); } //Mail the pdf and delete it // .... call mail method here return null; }
有关更多信息,请访问此处https://github.com/Purush0th/PDFnMail
上述就是C#学习教程:如何使用asp.net c将使用jsPDF生成的PDF附加到邮件中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/956391.html