Scu*_*Kay 3 progress download asp.net-mvc-4
我有一个调用此功能的下载按钮:
public FileResult DownloadExport()
{
string fileName = "example";
// Activate 'In progress'
// Call to a function that takes a while
// Deactivate 'In progress'
return File(fileName, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(fileName));
}
Run Code Online (Sandbox Code Playgroud)
所以,我调用一个为我生成文件的函数.此功能需要一段时间,我不希望我的用户认为应用程序崩溃.这就是为什么我想在用户等待时显示"进行中"的原因.我该如何实现呢?
澄清:这个问题不是关于下载的进度,而是关于生成文件的函数的进度.
我最近有这个完全相同的问题,我相信我的解决方案(根据对相关问题的答案)是你正在寻找的这个问题.基本思想是在生成文件时显示进度微调器(在我的情况下),在文件生成完成时隐藏进度微调器,然后提供要下载的文件.要做到这一点,我需要四件事:
首先,控制器上的操作需要将文件存储在Session中
[HttpPost]
public ActionResult RunReport(ReportViewModel viewmodel)
{
// Process that generates the object while will become the file here
// ...
using (var stream = new MemoryStream())
{
// Convert the object to a memory stream
report.Generate(stream); // Use your object here
string handle = Guid.NewGuid().ToString();
Session[handle] = stream.ToArray();
return new JsonResult()
{
Data = new
{
FileGuid = handle,
MimeType = "application/pptx",
FileName = "My File.pptx"
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
控制器还需要一个新的动作,它将提供实际的下载文件
public ActionResult Download(string fileGuid, string mimeType, string filename)
{
if(Session[fileGuid] != null)
{
byte[] data = Session[fileGuid] as byte[];
Session.Remove(fileGuid); // Cleanup session data
return File(data, mimeType, filename);
}
else
{
// Log the error if you want
return new EmptyResult();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,从视图中显示进度微调器的AJAX调用,调用RunReport(需要很长时间的操作),使用它返回的JSON数组返回下载文件(这是一个快速操作),然后隐藏微调器再次.
<script type="text/javascript">
function RunReport(reportUrl) {
$.ajax({
cache: false,
url: reportUrl,
type: "POST",
success: function(response) {
window.location = "/Report/Download?fileGuid=" + response.FileGuid +
"&mimeType=" + response.MimeType + "&filename=" + response.FileName;
$("#progress-spinner").hide();
}
});
$("#progress-spinner").show();
}
</script>
Run Code Online (Sandbox Code Playgroud)
最后,启动它的链接并生成用于AJAX调用的操作链接
<a href="javascript: RunReport('@Url.Action("RunReport", "UserReport", new { ReportId = Model.Id })')">Run Report</a>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助别人!
| 归档时间: |
|
| 查看次数: |
10702 次 |
| 最近记录: |