【问题标题】:Reload page after file download via Response通过响应下载文件后重新加载页面
【发布时间】:2014-10-22 06:04:16
【问题描述】:

我正在编写一个报告工具,您可以在其中直接在页面上显示报告或将其下载为 excel 文件。如果您只想在页面上显示它,则网站会按预期重新加载。如果将其下载为 excel 文件,则下载有效,但不会重新加载页面。这对我来说是个问题,因为我不知道之后如何禁用加载动画。下载是通过对响应对象的写入操作来完成的。代码如下:

private void ExcelExport(DataTable outList)
{
     ViewBag.Reload = true;
     Response.ClearContent();
     Response.Buffer = true;
     Response.AddHeader("content-disposition", "attachment; filename=report.xls");
     Response.ContentType = "application/ms-excel";
     Response.Charset = "";
     Response.Output.Write(Excel.GetXml(outList));
     Response.Flush();
     Response.End();
}

代码是从我的控制器中的 ActionResult 方法调用的(ExcelExport 方法也位于其中):

public ActionResult WisPriceMatrix(string cc, string sl, string exp)
{
     ViewBag.StorageLocations = this.StorageLocations;
     ViewBag.WisPriceMatrixReport = null;
     int cleanCode = 3;
     if ((cc != null && cc != string.Empty && Int32.TryParse(cc, out cleanCode)) || (sl != null && sl != string.Empty))
     {
          sl = sl == string.Empty ? null : sl;

          ViewBag.ParameterSl = sl;
          ViewBag.ParameterCleanCode = cleanCode;

          DataTable outList = dc.GetWisPriceMatrix(sl, cleanCode);

          if (exp == null || exp == string.Empty || exp != "1")
          {
               ViewBag.WisPriceMatrixReport = outList;
          }
          else
          {
                if (outList.Count > 0)
                {
                     this.ExcelExport(outList);
                }
                else
                {
                     ViewBag.NoResults = "1";
                }
           }
    }

    return View();
}

我有什么想法可以在之后强制页面重新加载吗?

我尝试创建一个 ViewBag 变量来指示需要重新加载并通过 JavaScript 对其作出反应,但由于页面未刷新,因此这是不成功的 ;-)。

【问题讨论】:

  • 在此处发布更多代码,您正在调用 ExcelExport 方法...????
  • 我已经用相应的代码编辑了我的问题。
  • 不明白你到底想要什么???
  • 调用此 ExcelExport 方法后,视图未重新加载。相反,它只是保持在发送响应之前的状态。但我需要它重新加载。
  • 上图是你的post方法???

标签: asp.net asp.net-mvc refresh response


【解决方案1】:

在您的情况下,为了重新加载页面,您可以使用Viewbag 并在控制器上设置Viewbag 值,例如Viewbag.data="reload",然后在视图中检查Viewbag

$(document).ready(function(){
  if('@Viewbag.data' == "reload")
  {
    window.location.reload(true);
  }
});

或者您可以使用 return RedirectToAction("WisPriceMatrix") 代替 return View() 来代替 RedirectToAction 创建一个新的 http(302) 请求并重新加载页面。

【讨论】:

  • 我已经尝试过类似于第一个建议的东西。但这不起作用,因为站点没有重新加载,因此脚本不会再次执行。我刚刚尝试了第二个建议,它也不起作用。命令return RedirectToAction("WisPriceMatrix"); 已到达,但对结果没有影响。
  • @RomanoZumbé...第二个答案肯定会成功...只需检查您的 httpget 和 httppost 控制器操作是否具有名称“WisPriceMatrix”..??
  • 控制器操作名为“WisPriceMatrix”,但我认为手动编辑响应后RedirectToActionMethod 的行为与预期不符。
  • 好的,那么由于其他原因它无法正常工作。
  • 没有。正如我所说,在编辑响应后,任何操作的返回都没有结果。
猜你喜欢
  • 2013-10-26
  • 2019-07-20
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
相关资源
最近更新 更多