Web表单附加pdf,但无效?

时间:2014-06-29 23:55:47

标签: asp.net pdf webforms itextsharp email-attachments

使用Itext sharp pdf并有一个接受输入的表单,然后它接受输入,包括用户的电子邮件地址,制作pdf,然后发送一封电子邮件,其中pdf附在用户输入的电子邮件地址。也就是说,附件和pdf生成有效,但是当您下载或尝试打开pdf时,它不会打开或任何东西。这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Net.Security;
using System.Web.UI.HtmlControls;

namespace pacfam
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void ShowPdf(string filename)
        {
            //Clears all content output from Buffer Stream
            Response.ClearContent();
            //Clears all headers from Buffer Stream
            Response.ClearHeaders();
            //Adds an HTTP header to the output stream
            Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
            //Gets or Sets the HTTP MIME type of the output stream
            Response.ContentType = "application/pdf";
            //Writes the content of the specified file directory to an HTTP response output stream as a file block
            Response.WriteFile(filename);
            //sends all currently buffered output to the client
            Response.Flush();
            //Clears all content output from Buffer Stream
            Response.Clear();
        }

        protected void btnShow_OnClick(object sender, EventArgs e)
        {
            //create variable from input in textbox control
            var textInput = useIn.Text;

            //server folder path which is stored your PDF documents
            string path = Server.MapPath("PDF-Files");
            string filename = path + "/Doc1.pdf";

            //Create new PDF document 
            Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);

            PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

            document.Open();
            document.Add(new Paragraph(textInput));
            document.Close();

            ShowPdf(filename);

            //setups and builds email to send to user
            SmtpClient smtp = new SmtpClient("xxxx.xxxxxxx.com", 587);
            smtp.EnableSsl = false;
            smtp.UseDefaultCredentials = false;

            //grabs form data in memory for pdf to attach to email
            MemoryStream memNow = new MemoryStream();

            smtp.Credentials = new NetworkCredential("xxxx@xxxx.com", "password");
            MailMessage mail = new MailMessage("pacificfamily@blueskyprojects.com", TextBox2.Text, TextBox5.Text, TextBox6.Text);
            mail.Attachments.Add(new Attachment(memNow, "Doc1.pdf"));
            smtp.Send(mail);
        }

    }
}

实际表单的链接,因为这是后面的代码:

http://67.228.49.104:8080/surveymain

它附加了PDF并且我在服务器上确认已创建PDF(显示带有while大小的pdf),但是,显示通过电子邮件发送的PDF的文件大小(如果已下载),文件大小因此,不确定这里发生了什么,或者我的代码可能出错。

关于这里发生了什么的任何想法?

非常感谢。

1 个答案:

答案 0 :(得分:2)

正如@mkl所说,您正在创建一个全新的空MemoryStream并将其添加到假名Attachments的{​​{1}}集合中。您可以通过以下两种方式之一解决此问题。

1 - 使用实际文件

只需使用您生成的实际文件并附上该文件:

Doc1.pdf

2 - 完全绕过磁盘

除非您需要将文件存储到磁盘,否则您可以直接写入mail.Attachments.Add(new Attachment(filename)); ,然后在电子邮件中使用该文件。下面的代码是一个更激烈的变化,所以我也冒昧地切换到MemoryStream模式来处理对象处理。

using