如何下载多个文件?获取 "Server cannot clear headers after HTTP headers have been sent."
How to download multiple files? Getting "Server cannot clear headers after HTTP headers have been sent."
我有一个显示下载文件列表的 MVC 应用程序。用户选择带有复选框的文件,这些文件在服务器端使用 Pechkin 库生成为 PDF。
我的问题是,当下载多个文件时,我收到 "Server cannot clear headers after HTTP headers have been sent." 错误,我意识到这是因为我只能将单个 HTTP 响应发送回客户端,但我不确定如何解决这个问题。
public ActionResult Forms(FormsViewModel model)
{
foreach (var item in model.Forms)
{
if (item.Selected)
{
CreatePdfPechkin(RenderRazorViewToString(model.FormType, model.Form), model.Name);
}
}
return View(model);
}
private void CreatePdfPechkin(string html, string filename)
{
var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.1)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true), html);
Response.Clear();
Response.ClearContent();
// error on the next line for the second file to be downloaded
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf; size={1}", filename, pdf.Length));
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
}
我应该使用什么模式来完成这个?
您可以压缩所有选定的文件并将其发送。在名为 DotNetZip 的 codeplex 上有一个可用的库
这就是压缩它的方法。
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddFile("path to file one");
zip.AddFile("path to file two");
zip.AddFile("path to file three");
zip.AddFile("path to file four");
zip.Save(outputStream);
}
outputStream.Position = 0;
return File(outputStream, "application/zip","zip file name.zip");
我有一个显示下载文件列表的 MVC 应用程序。用户选择带有复选框的文件,这些文件在服务器端使用 Pechkin 库生成为 PDF。
我的问题是,当下载多个文件时,我收到 "Server cannot clear headers after HTTP headers have been sent." 错误,我意识到这是因为我只能将单个 HTTP 响应发送回客户端,但我不确定如何解决这个问题。
public ActionResult Forms(FormsViewModel model)
{
foreach (var item in model.Forms)
{
if (item.Selected)
{
CreatePdfPechkin(RenderRazorViewToString(model.FormType, model.Form), model.Name);
}
}
return View(model);
}
private void CreatePdfPechkin(string html, string filename)
{
var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.1)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true), html);
Response.Clear();
Response.ClearContent();
// error on the next line for the second file to be downloaded
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf; size={1}", filename, pdf.Length));
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
}
我应该使用什么模式来完成这个?
您可以压缩所有选定的文件并将其发送。在名为 DotNetZip 的 codeplex 上有一个可用的库 这就是压缩它的方法。
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddFile("path to file one");
zip.AddFile("path to file two");
zip.AddFile("path to file three");
zip.AddFile("path to file four");
zip.Save(outputStream);
}
outputStream.Position = 0;
return File(outputStream, "application/zip","zip file name.zip");