IActionResult从Controller返回文件和视图

时间:2018-01-04 18:49:16

标签: c# asp.net-mvc

是否可以返回两个文件以供下载并从IActionResult更新视图?

如果没有,是否有解决方法? (JavaScript或AJAX)

我希望在文件下载后刷新视图。

代码:

[HttpPost]
public IActionResult ProcessFile(ViewModel model, List<IFormFile> files)
{
    var uploadedfileName = UploadFile(files);
    try
    {
        if (uploadedfileName != null)
        {
            // Generate a report for download
            var fileInfo = GenerateReports(model.Host, uploadedfileName);

            // Prompt Download File
            var webClient = new System.Net.WebClient();
            var downloadData = webClient.DownloadData(fileInfo.ToString());
            var content = new System.IO.MemoryStream(downloadData);
            var contentType = "APPLICATION/octet-stream";
            var fileName = Path.GetFileName(fileInfo.ToString());
            return File(content, contentType, fileName);                    
        }
        else
        {
            ViewBag.Message = "Please select the file";
        }
    }
    catch (Exception e)
    {
        // Log Error
    }
    finally
    {
        // Delete the file after the report is generated
        System.IO.File.Delete(uploadedfileName);
    }
    return View("Index");
}

0 个答案:

没有答案