复制 headers 并在错误的路径中调用文件
Duplicate headers and Calling the file in the wrong path
我正在尝试通过单击 link 从我的网页创建和显示 pdf。但是当系统要显示的时候就出问题了。
当我使用这个命令时
return new 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
当我使用这个命令时:
return File(fileStream, "application/pdf", fullFileName);
然后我收到一条错误消息:
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);
}
如果我不得不 post 更多。我会做的。不过我觉得没必要。
您很可能需要 .Seek
流式传输到开头(通常当您写入文件时 Position
指向 end/last 写入位置)。
第二个问题有点不言自明,因为您设置了 "content-disposition" header 两次 - 手动并通过 File(...)
.
三件事:
您是否尝试删除(或 commenting-out)将 header 添加到响应中的行?返回 File(...) 很可能会为您添加 header,导致 header 被指定两次,这将导致您看到的错误。
文件 (...) 应指定 filename.pdf
,而不是文件的完整路径。
请注意,您的文件名不应包含任何空格或任何 non-alphanumeric 个字符。这是 RFC 6266, Appendix D 的 recommended-against。
这是我的有效结果:
这是从视图调用
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;
}
我正在尝试通过单击 link 从我的网页创建和显示 pdf。但是当系统要显示的时候就出问题了。
当我使用这个命令时
return new 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
当我使用这个命令时:
return File(fileStream, "application/pdf", fullFileName);
然后我收到一条错误消息:
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);
}
如果我不得不 post 更多。我会做的。不过我觉得没必要。
您很可能需要 .Seek
流式传输到开头(通常当您写入文件时 Position
指向 end/last 写入位置)。
第二个问题有点不言自明,因为您设置了 "content-disposition" header 两次 - 手动并通过 File(...)
.
三件事:
您是否尝试删除(或 commenting-out)将 header 添加到响应中的行?返回 File(...) 很可能会为您添加 header,导致 header 被指定两次,这将导致您看到的错误。
文件 (...) 应指定
filename.pdf
,而不是文件的完整路径。请注意,您的文件名不应包含任何空格或任何 non-alphanumeric 个字符。这是 RFC 6266, Appendix D 的 recommended-against。
这是我的有效结果:
这是从视图调用
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;
}