4

马上开始:我是使用 asp.net mvc 4 的新手。

我有一个创建 excel 文件然后将其转换为 PDF 的操作。

从视图

@Html.ActionLink("Generate Invoice", "genInvoice", new { id = item.invoiceID }) |

行动:

public ActionResult genInvoice(int id = 0)
    {
        var invoiceItems = from k in db.InvoiceItems
                           where k.invoiceID == id
                           select k;

        string invoiceClient = (from kk in db.Invoices
                                where kk.invoiceID == id
                                select kk.clientName).Single();


        invoiceClient = invoiceClient + "_" + DateTime.Now.ToString("ddd dd MMM yyyy hhTmm");
        string websitePath = Request.PhysicalApplicationPath;
        string pathName = websitePath + "\\" + invoiceClient ;
        generateInvoice(invoiceItems, pathName + ".xlsx", id);
        convertToPDF(pathName, invoiceClient);

//Response.AppendHeader("Content-Disposition", "attachment");

          var viewModel = new InvoiceItemAdd();
          viewModel.Invoices = db.Invoices
              .Include(i => i.InvoiceItems)
              .OrderBy(i => i.invoiceID);
        return View("Index", viewModel);
        //return RedirectToAction("Index",viewModel);


    }

现在我想最终下载 PDF 文件,然后返回索引视图。它进入索引视图打印 html 等,但随后窗口保持为带有 url 的白屏:/Invoice/genInvoice/1

知道我该怎么做吗?(PDF生成后返回索引视图,也下载它)

4

1 回答 1

1

对不起,我解决了白屏问题。尝试下载 PDF 时

//Response.AppendHeader("Content-Disposition", "inline; filename="+invoiceClient+".pdf");
            //Return File(output, "application/pdf");
            //Response.Flush();

            //Response.End();

Response.End() 没有被注释掉,我猜这停止了它。

现在的问题是如何在单独的选项卡中打开 PDF 并使用上述代码返回当前索引。

编辑:决定文件可以下载。

public FileResult genInvoice(int id = 0)
{
//More code
Response.AppendHeader("Content-Disposition", "attachment; filename="+pathName+".pdf");
return File(websitePath + "\\" + invoiceClient + ".pdf", "application/pdf");
}
于 2012-11-30T10:44:24.547 回答