C# 在线创建pdf文件,将其保存在服务器上,并将其作为电子邮件附件发送

C# 在线创建pdf文件,将其保存在服务器上,并将其作为电子邮件附件发送,c#,asp.net,C#,Asp.net,我有一个带有网格的web表单。我想生成整个web表单和网格数据的pdf,保存在服务器端本身,而不必像将数据导出到pdf时那样保存在本地计算机上,并将pdf作为电子邮件附件发送。当网站在服务器上运行时,我希望执行所有这些功能。我如何才能完成此任务 我有将网格数据转换为pdf的代码,但不知道如何在服务器上保存该pdf文件 我正在使用以下类型的代码转换为pdf private void Export_to_Pdf() { Response.ContentType = “application/pd

我有一个带有网格的web表单。我想生成整个web表单和网格数据的pdf,保存在服务器端本身,而不必像将数据导出到pdf时那样保存在本地计算机上,并将pdf作为电子邮件附件发送。当网站在服务器上运行时,我希望执行所有这些功能。我如何才能完成此任务

我有将网格数据转换为pdf的代码,但不知道如何在服务器上保存该pdf文件

我正在使用以下类型的代码转换为pdf

private void Export_to_Pdf()

{

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str = “<h1 title=’Header’ align=’Center’> Writing To PDF Using ASP.NET</h1> <br><table align=’Center’><tr><td style=’width:100px;color:green’> <b>iTextSharp</b></td><td style=’width:100px;color:red’>dotnetgeekblog</td></tr></table>”;
StringReader sr = new StringReader(str);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}
private void导出到Pdf()
{
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str=“使用ASP.NET写入PDF
iTextSharpdotnetgeekblog”; StringReader sr=新的StringReader(str); 文档pdfDoc=新文档(PageSize.A4、10f、10f、100f、0.0f); HTMLWorker htmlparser=新的HTMLWorker(pdfDoc); GetInstance(pdfDoc,Response.OutputStream); pdfDoc.Open(); 解析(sr); pdfDoc.Close(); 响应写入(pdfDoc); Response.End(); }
该文件刚在本地计算机上下载。我想将其保存在服务器上并通过电子邮件发送


提前谢谢

您可以使用
System.net
命名空间,如下所示:

public static void CreateMessageWithAttachment(string server)
    {
        MemoryStream file = // Your Memeory stream to send as attachment
        MailMessage message = new MailMessage(
           "abc@xyz.com",
           "pqr@xyz.com",
           "report.",
           "See the attached pdf.");

        Attachment data = new Attachment( file , "example.pdf", "application/pdf" );

        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

          client.Send(message);

        }

如果您不保存,如何将其作为附件发送电子邮件检查此问题:您必须在服务器端保存此pdf,并在邮件发送后删除或删除…关于您的下载,这可能取决于您的代码您可以删除下载代码…这对我有用我们使用第三方库(动态pdf生成器)在我们的网站上创建pdf。使用它很容易,但它不是免费的。@REx-您可以使用流连接。内存流也可以提供服务。