C# 重复的标题并在错误的路径中调用文件

C# 重复的标题并在错误的路径中调用文件,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在尝试通过单击链接从我的网页创建并显示pdf。但是当系统必须显示出它出了问题时 当我使用这个命令时 返回新的FileStreamResult(fileStream,“application/pdf”) 它显示一个空的pdf文件: ///C:/Users/Me/Downloads/C--Apps-MyWebSolution-MyWeb-Documenten-MyList_198721.pdf 它另存为:C:\Apps\MyWebSolution\MyWeb\Documenten\MyList_

我正在尝试通过单击链接从我的网页创建并显示pdf。但是当系统必须显示出它出了问题时

当我使用这个命令时

返回新的FileStreamResult(fileStream,“application/pdf”)

它显示一个空的pdf文件:

///C:/Users/Me/Downloads/C--Apps-MyWebSolution-MyWeb-Documenten-MyList_198721.pdf

它另存为:C:\Apps\MyWebSolution\MyWeb\Documenten\MyList_198721%20.pdf

当我使用这个命令时:

返回文件(fileStream,“application/pdf”,fullFileName)

然后我收到一条错误消息:

错误\u响应\u标题\u多个\u内容\u处置

我做错了什么

public FileStreamResult PDFGenerator(string html, string fileName)
{
    string fullFileName = Server.MapPath("~/Documenten/" + fileName + ".pdf");
    Stream fileStream = CreatePDF(html, fullFileName);

    HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + fullFileName);

    //return new FileStreamResult(fileStream, "application/pdf");
    return File(fileStream, "application/pdf", fullFileName);
}

如果我必须发布更多。我会的。但我认为不需要它。

最有可能需要
。查找
流到开头(通常在写入文件时
位置
指向结尾/最后写入的位置)


第二个问题有点不言自明,因为您需要手动和通过
文件(…)
设置“内容处置”标题两次。您最有可能需要
.Seek
流到开头(通常在写入文件时
位置
指向结尾/最后写入的位置)


第二个问题有点不言自明,因为您需要手动和通过
文件(…)
设置“内容处置”标题两次。您最有可能需要
.Seek
流到开头(通常在写入文件时
位置
指向结尾/最后写入的位置)


第二个问题有点不言自明,因为您需要手动和通过
文件(…)
设置“内容处置”标题两次。您最有可能需要
.Seek
流到开头(通常在写入文件时
位置
指向结尾/最后写入的位置)

第二个问题在某种程度上是不言自明的,因为您可以手动和通过
文件(…)
设置两次“内容处置”标题,这有三件事:

  • 您是否尝试删除(或注释掉)在响应中添加标题的行?返回文件(…)可能会为您添加头,导致头被指定两次,这将导致您看到的错误

  • 文件(…)应该指定
    filename.pdf
    ,而不是文件的整个路径

  • 请注意,文件名不应包含任何空格或任何非字母数字字符。这是建议反对的

  • 三件事:

  • 您是否尝试删除(或注释掉)在响应中添加标题的行?返回文件(…)可能会为您添加头,导致头被指定两次,这将导致您看到的错误

  • 文件(…)应该指定
    filename.pdf
    ,而不是文件的整个路径

  • 请注意,文件名不应包含任何空格或任何非字母数字字符。这是建议反对的

  • 三件事:

  • 您是否尝试删除(或注释掉)在响应中添加标题的行?返回文件(…)可能会为您添加头,导致头被指定两次,这将导致您看到的错误

  • 文件(…)应该指定
    filename.pdf
    ,而不是文件的整个路径

  • 请注意,文件名不应包含任何空格或任何非字母数字字符。这是建议反对的

  • 三件事:

  • 您是否尝试删除(或注释掉)在响应中添加标题的行?返回文件(…)可能会为您添加头,导致头被指定两次,这将导致您看到的错误

  • 文件(…)应该指定
    filename.pdf
    ,而不是文件的整个路径

  • 请注意,文件名不应包含任何空格或任何非字母数字字符。这是建议反对的


  • 这是我的有效结果:

    这是从视图调用

        public HttpResponseBase HitlijstNaarPdf(int? AID_Hitlijst = 0)
        {
            SelectPeriode(AID_Hitlijst);
            var model = new HitlijstModel();
    
            GetHitlijstSettings();
            string sHtmlInhoud = ViewRenderer.RenderView("Hitlijst", model, ControllerContext);
            string fileName = "Hitlijst_" + AID_Hitlijst.ToString() + ".pdf";
    
            return CreatePDF(sHtmlInhoud, fileName);
        }
    
        private HttpResponseBase CreatePDF(string html, string fileName)
        {
            string convertingTool = Server.MapPath("~/bin/wkhtmltopdf.exe ");
    
            ProcessStartInfo psi = new ProcessStartInfo(convertingTool);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
    
            Process myProcess = Process.Start(psi);
            myProcess.WaitForExit();
            myProcess.Close();
    
            string fullFileName = Server.MapPath("~/Documenten/" + fileName);
            if (!System.IO.File.Exists(fullFileName))
            {
                Document pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30);
    
                PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream(fullFileName, FileMode.Create));
                PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
                pdfDocument.Open();
    
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDocument, new StringReader(html));
                pdfDocument.Close();
            }
    
            Response.Clear();
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(fullFileName); 
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
    
            return Response;
        }
    

    这是我的有效结果:

    这是从视图调用

        public HttpResponseBase HitlijstNaarPdf(int? AID_Hitlijst = 0)
        {
            SelectPeriode(AID_Hitlijst);
            var model = new HitlijstModel();
    
            GetHitlijstSettings();
            string sHtmlInhoud = ViewRenderer.RenderView("Hitlijst", model, ControllerContext);
            string fileName = "Hitlijst_" + AID_Hitlijst.ToString() + ".pdf";
    
            return CreatePDF(sHtmlInhoud, fileName);
        }
    
        private HttpResponseBase CreatePDF(string html, string fileName)
        {
            string convertingTool = Server.MapPath("~/bin/wkhtmltopdf.exe ");
    
            ProcessStartInfo psi = new ProcessStartInfo(convertingTool);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
    
            Process myProcess = Process.Start(psi);
            myProcess.WaitForExit();
            myProcess.Close();
    
            string fullFileName = Server.MapPath("~/Documenten/" + fileName);
            if (!System.IO.File.Exists(fullFileName))
            {
                Document pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30);
    
                PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream(fullFileName, FileMode.Create));
                PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
                pdfDocument.Open();
    
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDocument, new StringReader(html));
                pdfDocument.Close();
            }
    
            Response.Clear();
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(fullFileName); 
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
    
            return Response;
        }
    

    这是我的有效结果:

    这是从视图调用

        public HttpResponseBase HitlijstNaarPdf(int? AID_Hitlijst = 0)
        {
            SelectPeriode(AID_Hitlijst);
            var model = new HitlijstModel();
    
            GetHitlijstSettings();
            string sHtmlInhoud = ViewRenderer.RenderView("Hitlijst", model, ControllerContext);
            string fileName = "Hitlijst_" + AID_Hitlijst.ToString() + ".pdf";
    
            return CreatePDF(sHtmlInhoud, fileName);
        }
    
        private HttpResponseBase CreatePDF(string html, string fileName)
        {
            string convertingTool = Server.MapPath("~/bin/wkhtmltopdf.exe ");
    
            ProcessStartInfo psi = new ProcessStartInfo(convertingTool);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
    
            Process myProcess = Process.Start(psi);
            myProcess.WaitForExit();
            myProcess.Close();
    
            string fullFileName = Server.MapPath("~/Documenten/" + fileName);
            if (!System.IO.File.Exists(fullFileName))
            {
                Document pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30);
    
                PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream(fullFileName, FileMode.Create));
                PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
                pdfDocument.Open();
    
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDocument, new StringReader(html));
                pdfDocument.Close();
            }
    
            Response.Clear();
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(fullFileName); 
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
    
            return Response;
        }
    

    这是我的有效结果:

    这是从视图调用

        public HttpResponseBase HitlijstNaarPdf(int? AID_Hitlijst = 0)
        {
            SelectPeriode(AID_Hitlijst);
            var model = new HitlijstModel();
    
            GetHitlijstSettings();
            string sHtmlInhoud = ViewRenderer.RenderView("Hitlijst", model, ControllerContext);
            string fileName = "Hitlijst_" + AID_Hitlijst.ToString() + ".pdf";
    
            return CreatePDF(sHtmlInhoud, fileName);
        }
    
        private HttpResponseBase CreatePDF(string html, string fileName)
        {
            string convertingTool = Server.MapPath("~/bin/wkhtmltopdf.exe ");
    
            ProcessStartInfo psi = new ProcessStartInfo(convertingTool);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
    
            Process myProcess = Process.Start(psi);
            myProcess.WaitForExit();
            myProcess.Close();
    
            string fullFileName = Server.MapPath("~/Documenten/" + fileName);
            if (!System.IO.File.Exists(fullFileName))
            {
                Document pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30);
    
                PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream(fullFileName, FileMode.Create));
                PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
                pdfDocument.Open();
    
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDocument, new StringReader(html));
                pdfDocument.Close();
            }
    
            Response.Clear();
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(fullFileName); 
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
    
            return Response;
        }
    

    谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释谢谢你的回答和解释