简体   繁体   English

导出 Excel 数据时重定向到操作不起作用

[英]Redirect to action doesn't work when exporting Excel data

public ActionResult FileDownload(Files FileData)
{
    dynamic data = FileDownload(FileData);
    var Platform= Enum.Parse(typeof(Platform),FileData.PlatformNo.ToString());
    var fileName = "_Orders_" + FileData.CountryCode + "_" + FileData.Month 
                   + "_" + FileData.Year + "_" + Platform + "_" + FileData.Currency;
    var workSheetName = "_File";

    ExcelHelper.ExportToExcel(data, workSheetName, fileName, "A1:Y1", Response, 6, 7, 8);

    return RedirectToAction("AzureDetail", new { id = FileData.FileId, FileData });
}

<td>
  <a ng-click="post('/Plan/FileDownload',FileData)" class="btn btn-primary btn-xs">Download</a>
</td>

By running this piece of code, I am not able to debug into azure detail view, it doesn't debug RedirectToAction .通过运行这段代码,我无法调试到 azure 详细视图,它不会调试RedirectToAction

Moreover I want to download the Excel file and perform page refresh at the same time.此外,我想下载 Excel 文件并同时执行页面刷新。 I have gone through many solutions that say "both page refresh and download can't be handled in one response" or "you can handle this in JavaScript by setTimeout() ".我经历了许多解决方案,说“页面刷新和下载都不能在一个响应中处理”或“您可以通过setTimeout()在 JavaScript 中处理此问题”。

Is there anything that I can use only at the code behind to do this?有什么我只能在后面的代码中使用的东西吗?

The action can only have one single result.该操作只能有一个结果。 You cannot download a file and redirect to another action at the same time, because those are two action results.您不能同时下载文件并重定向到另一个操作,因为这是两个操作结果。

What many websites do is they open the action to download the file in a popup tab/window.许多网站所做的是他们在弹出选项卡/窗口中打开下载文件的操作。 This way, your current window is still able to run JavaScript/Ajax to do other actions like redirecting to another page.这样,您当前的 window 仍然能够运行 JavaScript/Ajax 来执行其他操作,例如重定向到另一个页面。 But this solution has the downside of using popups which modern browsers block, so the user has to accept allowing popups (temporary or permanently) for your website.但是这种解决方案的缺点是使用现代浏览器阻止的弹出窗口,因此用户必须接受允许您网站的弹出窗口(临时或永久)。

As you mentioned in your question, you can also handle this in JavaScript, either by using setTimeout() or other mechanisms.正如您在问题中提到的,您也可以使用setTimeout()或其他机制在 JavaScript 中处理此问题。 As you can see, any solution for this dilemma involves using JavaScript in one way or another.如您所见,任何解决此困境的方法都涉及以一种或另一种方式使用 JavaScript。 That's because the server can only respond to the client with one response.那是因为服务器只能用一个响应来响应客户端。 So JavaScript must be used to send more than one request to the server.所以必须使用 JavaScript 向服务器发送多个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM