Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.net MVC返回文件和重定向_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc ASP.net MVC返回文件和重定向

Asp.net mvc ASP.net MVC返回文件和重定向,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有一个返回csv文件的控制器。我想在显示后重定向到操作,即返回主视图。 控制器逻辑如下所示: public ActionResult DownloadCSV(string fileName) { string csv; using (StreamReader sr = new StreamReader(fileName)) { csv = sr.ReadToEnd(); } return File(new System.Text.UTF8

我有一个返回csv文件的控制器。我想在显示后重定向到操作,即返回主视图。 控制器逻辑如下所示:

public ActionResult DownloadCSV(string fileName)
{
    string csv;
    using (StreamReader sr = new StreamReader(fileName))
    {
        csv = sr.ReadToEnd();
    }
    return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Export.csv");
}

简短的版本是,由于HTTP限制,您不能在同一操作中强制文件下载和重定向。但是您可以通过使用
窗口打开文件下载操作来强制下载。open

示例:

<a href="Action/RedirectPage" data-file="Action/DownloadCVS" class="file-download">Download File</a>

<script>
  $(function() {
      $('a.file-download').click(function() {
         window.open($(this).data('file'));
      }); 
  });
</script>

$(函数(){
$('a.file-download')。单击(函数(){
window.open($(this.data('file'));
}); 
});

在本例中,我使用了HTML5属性,但不限于此。

HTTP不支持此操作。您好,我的意思是此操作是从另一个创建文件的控制器调用的,而不是从viewreturn RedirectToAction(“DownloadCSV”,new{fileName=exportedCancelFile})调用的;