下载完成后如何重定向到其他页面?

时间:2017-03-28 17:38:00

标签: asp.net pdf redirect

以下是我将HTML转换为PDF的代码,我试图实现的是pdf下载后我希望将页面重定向到另一个页面:SendEmail.aspx我将通过电子邮件发送下载的pdf,Is有没有办法做到这一点?

我尝试使用Response.Redirect(“SendEmail.aspx”);并且还删除了Response.End,但是其中任何一个都在工作,要么重定向,要么下载pdf,有没有办法可以做到这两点?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using System.Data;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;

namespace HTMLtoPDF
{
public partial class HTMLtoPDF : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnClick_Click(object sender, EventArgs e)
    {

        DownloadAsPDF();
    }

    public void DownloadAsPDF()
    {
        try
        {
            string strHtml = string.Empty;
            string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "CASEID2.pdf";
            //string testPath = Server.MapPath("~/files/test.pdf");

            string template = File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));



            string case_id = "12345";
            string student_id = "";
            string student_name_input = "";
            string campus_input = "Falls Church";
            string email_input = "xyz@gmail.com";
            string phone_input = "";
            string term = "2";
            string address_input = "";
            string course_input = "Mobile Application Development";
            string reason_appeal = "Family Problems";
            string faculty_name = "XYZ";
            string remaining_work = "Task-1,Task-2,Task-3";
            string deadline_date = "May 1st";

            template = template.Replace("[CASEID]", case_id);
            template = template.Replace("[STUDENTID]", student_id);
            template = template.Replace("[STUDENTNAME]", student_name_input);
            template = template.Replace("[CAMPUS]", campus_input);
            template = template.Replace("[EMAIL]", email_input);
            template = template.Replace("[PHONE]", phone_input);
            template = template.Replace("[TERM]", term);
            template = template.Replace("[ADDRESS]", address_input);
            template = template.Replace("[COURSEID] [COURSENAME]", course_input);
            template = template.Replace("[REASON]", reason_appeal);
            template = template.Replace("[FACULTYNAME]", faculty_name);
            template = template.Replace("[REMAININGCOURSEWORK]", remaining_work);
            template = template.Replace("[DEADLINE]",deadline_date);



          //template.Replace("[DEADLINE]", Request[deadline_date]);



            //StringWriter sw = new StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(sw); 
            //dvHtml.RenderControl(hw);
            //StringReader sr = new StringReader(sw.ToString());

            //strHtml = sr.ReadToEnd();
            //sr.Close();

            //string temp2 = template.Replace("<!DOCTYPE html>\r\n<html>\r\n<head>\r\n    <title></title>\r\n\t<meta charset=\"utf-8\" />\r\n</head>\r\n<body>\r\n", "");



            CreatePDFFromHTMLFile(template, pdfFileName);

            Response.ContentType = "application/x-download";
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "CASEID2.pdf"));
            Response.AddHeader("Refresh", "2;URL=SendEmail.aspx");
            Response.WriteFile(pdfFileName);
            Response.Flush();
            Response.End();




        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
    {
        try
        {
            object TargetFile = FileName;

            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;




            GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
            GeneratePDF.HtmlPdfPage first = builder.AddPage();
            first.AppendHtml(HtmlStream);
            byte[] file = builder.RenderPdf();
            File.WriteAllBytes(TargetFile.ToString(), file);

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
            ModifiedFileName = TargetFile.ToString();
            ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

            iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
            reader.Close();
            if (File.Exists(TargetFile.ToString()))
                File.Delete(TargetFile.ToString());
            FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
            File.Copy(ModifiedFileName, FinalFileName);
            if (File.Exists(ModifiedFileName))
                File.Delete(ModifiedFileName);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
}

0 个答案:

没有答案