私は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)
{
}
}
。
それを削除して、サーバー側の上にそれを使用することができます。 –
その後、どうしたらいいですか? @SergeK。 –
ファイルをダウンロードした後で削除したい場合は、 'Download'機能の中で削除部分を移動してください。それ以外の場合は、ダウンロードが終了し、あなたが解決しているものがあれば、削除コードが決して呼び出されないようにすることができます。 –