javascript-在jQueryAjax中成功之前先完成调用

javascript-在jQueryAjax中成功之前先完成调用,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我正在编写jQuery代码来下载文件,下载后文件必须从服务器上删除。下面是我下载和删除文件的代码 if (method == "ValueAddedReportExportToExcel") { $.ajax({ async: false, type: "post", cache: false, url: '@Url.Action("ValueAddedReportExportToExcel"

我正在编写jQuery代码来下载文件,下载后文件必须从服务器上删除。下面是我下载和删除文件的代码

if (method == "ValueAddedReportExportToExcel") {                
    $.ajax({
        async: false,
        type: "post",
        cache: false,
        url: '@Url.Action("ValueAddedReportExportToExcel", "report")',
        data: {
            fromDate: $('#txtFromDate').val(),
            toDate: $('#txtToDate').val(),
            reportForWhom: $("#ddlReportForWhom").val(),
            customers: (ddlCustomers != null) ? ddlCustomers.join(',') : "",
            salesReps: (salesReps != null) ? salesReps.join(',') : "",
            users: (users != null) ? users.join(',') : "",
            emailTo: emailTo,
            subject: subject,
            body: body,
        },
        success: function (data) {
            fileName = data.fileName;

            // call to download action.           
            window.location = '@Url.Action("Download", "Report")' + '?file=' + data.fileName;
            console.log('Success Call');
        },
        complete: function () {
            console.log('Complete Call');
            $.ajax({
                async: false,
                type: "post",
                url: '@Url.Action("DeleteFile", "Report")',
                data: { file: filename },
                success: function () {
                    alert(filename + ' is deleted successfuly. ');
                }
            });
        }
    });
    //methodURL = '@Url.Action("ValueAddedReportExportToExcel", "report")';
}
以下两个功能用于控制器中的下载和删除功能

public virtual ActionResult Download(string file)
{
    string fullPath = Path.Combine(Server.MapPath("~/CostSavingReport"), file);
    byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
    //return File(fileBytes, "application/vnd.ms-excel", file);
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file);
}

public virtual void DeleteFile(string file)
{
    try
    {
        var fullPath = Path.Combine(Server.MapPath("~/CostSavingReport"), file);
        if (System.IO.File.Exists(fullPath))
        {
            System.IO.File.Delete(fullPath);
        }
    }
    catch (Exception)
    {
    }
}

现在的问题是首先调用
DeleteFile
操作,而不是
Download
操作如何首先调用
Download
,然后调用
DeleteFile

,您可以为该操作创建自定义属性,如下所示,该操作在执行方法后执行

public class DeleteFileAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
        // Delete file 
    } 
} 
用它来代替你的行动

[DeleteFileAttribute]
public virtual ActionResult Download(string file)
{
    string fullPath = Path.Combine(Server.MapPath("~/CostSavingReport"), file);
    byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
    //return File(fileBytes, "application/vnd.ms-excel", file);
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file);
}

在服务器端删除它。然后,我该怎么做@SergeK.如果要在下载文件后删除该文件,请将删除部分移动到
下载
功能中。否则,您依赖的客户端可以在下载结束后离开页面,并且无论您的解决方案是什么,删除代码都不会被调用。抱歉,但您不太熟悉ActionFilters,您能告诉我们在哪里编写文件删除代码吗?在DeleteFile或OnActionExecuted中?您必须在“public override void OnActionExecuted(ActionExecutedContext filterContext)”方法中编写删除代码,该方法将在下载完成执行后立即调用。@wacky_coder您可以参考更多参考谢谢!!这个动作过滤器对我有用。非常感谢。(y)