Hide image using Javascript after controller actio

2019-04-28 03:40发布

问题:

My application has been implemeted using MVC 3, .net. I am trying to generate an excel file at the click of a button. The call to the controller action is made using Ajax. My main problem is: During the file generation i am trying to display an image on the screen to let the user know of the ingoing operation. I can very well display the image but i cannot hide it after the operation is completed. The codei am using is :

Javascript code:

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

Controller Action code:

public ActionResult DownExcelReportForAssortment(Guid parameter)   
{

       try
       {

           //the contents for the file generation are fetched here..   
           // Write contents to excel file
           if (memoryStream != null)
           {
                var documentName = "Report.xls";
                byte[] byteArrary = memoryStream.ToArray();
                return File(byteArrary, "application/vnd.ms-excel", documentName);
           }
       }
       catch (Exception ex)
       {
           LogManager.LogException(ex);
       }
}

I do not return a Json result to the calling javascript method where i can write the code to hide the image. I am returning a file which can be saved by the user and the action is completed.

Can somone please suggect/help me of how can i hide the image once the file generation operation is complete?

Appreciate the help...

回答1:

You may checkout the following article and put this into action. So we start by defining a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult DownExcelReportForAssortment(Guid parameter, string tokenId)
    {
        // Simulate some heavy work to fetch the report
        Thread.Sleep(5000);

        // we fake it
        byte[] byteArray = System.IO.File.ReadAllBytes(@"c:\test.xls");

        var cookie = new HttpCookie("fileDownloadToken", tokenId);
        Response.AppendCookie(cookie);

        return File(byteArray, "application/vnd.ms-excel", "report.xls");
    }
}

and in the view:

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

Now the last step is to include the jquery.cookie plugin:

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

and write a script to subscribe to the click event of the anchor and track the download progress:

$(function () {
    var fileDownloadCheckTimer;

    $('.download').click(function () {
        var token = new Date().getTime();
        $(this).attr('href', function () {
            return this.href.replace('__token__', token);
        });

        // Show the download spinner
        $('body').append('<span id="progress">Downloading ...</span>');

        // Start polling for the cookie
        fileDownloadCheckTimer = window.setInterval(function () {
            var cookieValue = $.cookie('fileDownloadToken');
            if (cookieValue == token) {
                window.clearInterval(fileDownloadCheckTimer);
                $.cookie('fileDownloadToken', null);

                // Hide the download spinner
                $('#progress').remove();
            }
        }, 1000);
    });
});