我有一个带网格的网页表单。我想生成整个网页表单和网格数据的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();
}
该文件刚下载到本地机器上。我想将其保存在服务器上并通过电子邮件发送。
提前致谢。
答案 0 :(得分:0)
您可以使用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);
}