EmailContentDAL example = new EmailContentDAL();
example.Pdf(sMailBody);
EmailContentDAL是一个类名。Pdf是一种非静态方法,但是Response在pdf方法中不起作用
public void Pdf(string sMailBody)
{
StringReader sr = new StringReader(sMailBody.ToString()));
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=GetPass_" + passno + ".pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
答案 0 :(得分:2)
考虑到我们没有看到任何错误消息,我只是提供建议:
为什么不使用Response对象作为Pdf
方法的参数?这样做不会将Reponse
依赖项隐藏在Pdf
方法内。
public void Pdf(string sMailBody, Response currentResponse)
{
StringReader sr = new StringReader(sMailBody.ToString() ));
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc,
currentResponse.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
currentResponse.Clear();
currentResponse.ContentType = "application/pdf";
currentResponse.AddHeader("Content-Disposition", "attachment; "+
"filename=GetPass_" + passno + ".pdf");
currentResponse.Buffer = true;
currentResponse.Cache.SetCacheability(HttpCacheability.NoCache);
currentResponse.Write(pdfDoc);
currentResponse.End();
}
调用方法:
example.Pdf(sMailBody, Response);
通过使用这种方法,您可以通过模拟Response
对象来轻松地测试您的方法。
答案 1 :(得分:0)
您随时可以在System.Web.HttpContext.Current.Response.xxx
方法中使用Pdf
。
System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
或在方法中声明一次
HttpResponse Response = HttpContext.Current.Response;
然后,您可以像平常一样写Response.xxx
。