下载Excel后JavaScript回调功能



下载.csv文件后,我需要将邮件发送给用户。我不确定文件下载后如何设置回调功能。

$.ajax({
    url: '@Url.Action("GetAllCustomer", "Customers")',
    type: 'POST',
    data: { 'customerIds': strCustomerId },
    success: function (result) {
        if (result == "Success") {
            location.href = '@Url.Action("DownloadFile", "Customers", new { extension = "csv"})';
        } else {
            toastLast = toastr.error("No data found", "Generating File");
        }
    }
});

在第一次通话中,我在上述代码中获得了所有客户。关于成功回调,我正在调用下载文件方法以下载CSV文件。我需要在下载文件后发送邮件,但我不确定如何知道该文件已下载。还是我可以以其他方式实现。

请在下面找到我的下载文件方法。

public ActionResult DownloadFile(string extension)
    {
        var dateTime = DateTime.Now.ToString("M.dd.yy");
        var fileName = dateTime + " Application File." + extension;
        var array = TempData["Output"] as byte[];
        if (array != null)
        {
            var file = File(array, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            return file;
        }
        else
        {
            return new EmptyResult();
        }
    }

不要使用此行

   location.href ='@url.action(" downloadfile","客户",new {extension =" csv"})';

而是使用ajax请求到操作方法

   $ .ajax({        类型:" post",        url:'@url.action(" downloadfile"," customers")',        contentType:"应用/json; charset = utf-8",        数据类型:" JSON",        成功:function(){           //添加您的Sccuess操作        }        错误:function(){           //处理错误        }    });

最新更新