2017-10-17 9 views
1

私はjQueryコードをダウンロードファイルに書き込んでおり、ダウンロードファイルはサーバーから削除する必要があります。以下は、ファイルのダウンロードと削除のための私のコードです。javascript - jquery ajaxの成功部分の前に完全な呼び出しを完了

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

以下の2つの機能は、コントローラのダウンロードと削除の機能です。さて、問題はDeleteFileアクションが最初Downloadにし、それDeleteFile後に電話をかけるために何をすべきかを代わりにDownloadアクションの最初に呼ばれている

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

+0

それを削除して、サーバー側の上にそれを使用することができます。 –

+0

その後、どうしたらいいですか? @SergeK。 –

+0

ファイルをダウンロードした後で削除したい場合は、 'Download'機能の中で削除部分を移動してください。それ以外の場合は、ダウンロードが終了し、あなたが解決しているものがあれば、削除コードが決して呼び出されないようにすることができます。 –

答えて

3

あなたはその下のようなそのアクションを実行するためにあなたの方法を

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

申し訳ありませんが、ActionFiltersにはあまり知られていませんが、ファイルの削除コードを書き込む場所を明かすことができますか? DeleteFileまたはOnActionExecutedのいずれか? –

+0

"public override void OnActionExecuted(ActionExecutedContext filterContext)"メソッドで削除コードを記述する必要があります。このメソッドは、ダウンロードの実行が終了した直後に呼び出されます。 –

+0

@wacky_coder詳しくはhttps://msdn.microsoft.com/en-us/library/gg416513(VS.98).aspxを参照してください –

関連する問題