如何安装使用asp.net用c#jsPDF到邮件生成的PDF [英] How to attach a PDF generated using jsPDF to the mail using asp.net c#

查看:750
本文介绍了如何安装使用asp.net用c#jsPDF到邮件生成的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道是否有任何方式附加使用jsPDF生成的PDF文件,并​​在asp.net C#中的邮件呢?

I need to know if there is any way to attach a PDF file generated using jsPDF and mail it in asp.net C#?

我在C#中的以下code

I have the following code in 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库如下:

and I'm using a JsPDF library as follows:

<script type="text/javascript" src="jsPdf/jspdf.min.js"></script>
<script type="text/javascript">
    function getPDF()
    {
        var doc = new jsPDF();
        doc.text(20, 20, 'TEST Message');
        doc.addPage();
        //doc.save('volt.pdf');
    }
</script>

有没有办法将其附着在邮件发送前呢?
提前致谢。

Is there any way to attach it in the mail before send it? Thanks in advance.

推荐答案

您不能调用从服务器code(C#)客户端code(JavaScript函数)。
您只能通过(HTTP / HTTPS)协议进行通信。

You cannot call client-side code (Javascript function) from server code (c#). You can only communicate via the (HTTP/HTTPs) protocol.

我认为你需要生成客户端的PDF,然后将该PDF发送给服务器,这样就可以将PDF附加到电子邮件中。

I think you need to generate the PDF from the client and then send that PDF to server so that you can attach the PDF to an email.

在这种情况下,您需要首先生成PDF并将其发送到服务器为Base64字符串。

In that case you need to first generate the PDF and send it to the server as a base64 string.

您可以再转换在的base64 字符串 PDF 在C#和邮寄作为附件。

You can then convert the base64 string to PDF in C# and mail it as an attachment.

客户端:

function generatePdf() {    
    var doc = new jsPdf();
    doc.text("jsPDF to Mail", 40, 30);    
    var binary = doc.output();
    return binary ? btoa(binary) : "";

}

在发布的的base64 的PDF内容到服务器:

Posting the base64 pdf content to the server:

  var reqData = generatePdf();
$.ajax({
                url:url,
                data: JSON.stringify({data:reqData}),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success:function(){}
       });

在服务器(MVC控制器):

On the server (MVC Controller):

        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

这篇关于如何安装使用asp.net用c#jsPDF到邮件生成的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆