asp.net-mvc-3 – 在控制器操作完成后使用Javascript隐藏图像MVC3

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 在控制器操作完成后使用Javascript隐藏图像MVC3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序已经使用MVC 3,.net实现.
我想点击一个按钮生成一个excel文件.
使用Ajax调用控制器操作.
我的主要问题是:在文件生成期间,我试图在屏幕上显示图像,让用户知道进入操作.我可以很好地显示图像,但操作完成后我无法隐藏它.我使用的代码是:

Javascript代码

  1. $("input.DownloadExcelReport").click(function (e) {
  2. e.preventDefault();
  3. var parameter = -- code to fetch parameter value;
  4. var outputViewUrl = (the url is created here);
  5. showLoading(); -- This function displays the image
  6. window.location.href = outputViewUrl;
  7. });

控制器动作代码

  1. public ActionResult DownExcelReportForAssortment(Guid parameter)
  2. {
  3.  
  4. try
  5. {
  6.  
  7. //the contents for the file generation are fetched here..
  8. // Write contents to excel file
  9. if (memoryStream != null)
  10. {
  11. var documentName = "Report.xls";
  12. byte[] byteArrary = memoryStream.ToArray();
  13. return File(byteArrary,"application/vnd.ms-excel",documentName);
  14. }
  15. }
  16. catch (Exception ex)
  17. {
  18. LogManager.LogException(ex);
  19. }
  20. }

我没有将Json结果返回给调用javascript方法,在那里我可以编写代码来隐藏图像.
我正在返回一个文件,可以由用户保存并完成操作.

可以somone请suggect /帮助我如何在文件生成操作完成后隐藏图像?

感谢帮助……

解决方法

您可以查看 following article并将其付诸实施.所以我们首先定义一个控制器:
  1. public class HomeController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. return View();
  6. }
  7.  
  8. public ActionResult DownExcelReportForAssortment(Guid parameter,string tokenId)
  9. {
  10. // Simulate some heavy work to fetch the report
  11. Thread.Sleep(5000);
  12.  
  13. // we fake it
  14. byte[] byteArray = System.IO.File.ReadAllBytes(@"c:\test.xls");
  15.  
  16. var cookie = new HttpCookie("fileDownloadToken",tokenId);
  17. Response.AppendCookie(cookie);
  18.  
  19. return File(byteArray,"report.xls");
  20. }
  21. }

并在视图中:

  1. @Html.ActionLink(
  2. "download report","DownExcelReportForAssortment","Home",new { parameter = Guid.NewGuid(),tokenId = "__token__" },new { @class = "download" }
  3. )

现在最后一步是包含jquery.cookie插件

  1. <script type="text/javascript" src="@Url.Content("~/scripts/jquery.cookie.js")"></script>

并编写一个脚本来订阅锚点击事件并跟踪下载进度:

  1. $(function () {
  2. var fileDownloadCheckTimer;
  3.  
  4. $('.download').click(function () {
  5. var token = new Date().getTime();
  6. $(this).attr('href',function () {
  7. return this.href.replace('__token__',token);
  8. });
  9.  
  10. // Show the download spinner
  11. $('body').append('<span id="progress">Downloading ...</span>');
  12.  
  13. // Start polling for the cookie
  14. fileDownloadCheckTimer = window.setInterval(function () {
  15. var cookieValue = $.cookie('fileDownloadToken');
  16. if (cookieValue == token) {
  17. window.clearInterval(fileDownloadCheckTimer);
  18. $.cookie('fileDownloadToken',null);
  19.  
  20. // Hide the download spinner
  21. $('#progress').remove();
  22. }
  23. },1000);
  24. });
  25. });

猜你在找的asp.Net相关文章