簡體   English   中英

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

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

我正在寫一個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操作如何進行首先調用DownloadDeleteFile

您可以為該操作創建自定義屬性,如下所示,在執行方法后執行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM