简体   繁体   English

javascript - 在jquery ajax成功之前完成第一次调用

[英]javascript - Complete call first before success part in jquery ajax

I am writing a jQuery code to download file and after download file must delete from server. 我正在写一个jQuery代码下载文件,下载文件必须从服务器删除。 Below is my code for file download and delete file. 下面是我的文件下载和删除文件的代码。

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")';
}

And below two functions are for download and delete function in controller. 以下两个函数用于控制器中的下载和删除功能。

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)
    {
    }
}

Now the problem is first DeleteFile action is called instead of Download action what to do to make call first to Download and after it DeleteFile . 现在的问题是首先调用DeleteFile操作而不是Download操作如何进行首先调用DownloadDeleteFile

you can create custom attribute for that action like below that executes after your method is been executed 您可以为该操作创建自定义属性,如下所示,在执行方法后执行

public class DeleteFileAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
        // Delete file 
    } 
} 

and use it over your action 并将其用于您的行动

[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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM