标头重复,并在错误的路径中调用文件 [英] Duplicate headers and Calling the file in the wrong path
问题描述
我试图通过单击链接从我的网页创建并显示pdf.但是当系统必须显示它出问题时.
I'm trying to create and showing a pdf from my webpage by clicking a link. But when the system has to show it goes something wrong.
使用此命令时
返回新的FileStreamResult(fileStream,"application/pdf");
return new FileStreamResult(fileStream, "application/pdf");
它显示了一个空的pdf文件:
It shows a empty pdf file:
///C:/用户/我/下载/C--Apps-MyWebSolution-MyWeb-Documenten-MyList_198721.pdf
///C:/Users/Me/Downloads/C--Apps-MyWebSolution-MyWeb-Documenten-MyList_198721.pdf
它保存为:C:\ Apps \ MyWebSolution \ MyWeb \ Documenten \ MyList_198721%20.pdf
It is saved as: C:\Apps\MyWebSolution\MyWeb\Documenten\MyList_198721%20.pdf
当我使用此命令时:
返回File(fileStream,"application/pdf",fullFileName);
return File(fileStream, "application/pdf", fullFileName);
然后我收到一条错误消息:
then I get an error message:
ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
我在做什么错了?
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);
}
如果我不得不发布更多内容.我会做的.但我认为这不是必需的.
If I had to post more. I'll do it. But I don't think it is needed.
推荐答案
这是我的有效结果:
这是从视图调用的
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;
}
这篇关于标头重复,并在错误的路径中调用文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!