How can I validate from my next code js
that when the answer is null
do not download the file excel
and be able to display a message. I already generate the excel
with the data but in the case that it does not contain anything the query downloads the excel
empty and I want to validate that.
My JS
function ExportToExcel() {
$('#btn-ExportarExcelTab1').on('click', function () {
var codplaza = $('#Codigo-Plaza').val();
ajax_download("@Url.Action("CrearExcel", "ImpAuditoriaCinta")", {'cod': cod}, 'ASMCli');
});
}
function ajax_download(url, data, input_name) {
var $iframe,
iframe_doc,
iframe_html;
if (($iframe = $('#download_iframe')).length === 0) {
$iframe = $("<iframe id='download_iframe'" +
" style='display: none' src='about:blank'></iframe>"
).appendTo("body");
}
iframe_doc = $iframe[0].contentWindow || $iframe[0].contentDocument;
if (iframe_doc.document) {
iframe_doc = iframe_doc.document;
}
iframe_html = "<html><head></head><body><form method='POST' action='" +
url + "'>" +
"<input type=hidden name='" + input_name + "' value='" +
JSON.stringify(data) + "'/></form>" +
"</body></html>";
iframe_doc.open();
iframe_doc.write(iframe_html);
$(iframe_doc).find('form').submit();
}
Controller:
public ActionResult CrearExcel(String cod)
{
try
{
byte[] fileBytes = _IAdi.ExportExcel(cod); // aqui hago la consulta y creo el excel
if (fileBytes == null) //aqui corto el procedimiento si es nulo si la consulta no contiene nada en IAdi.ExportExcel(cod)
return null;
string fileName = "Report.xlsx";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
catch (Exception)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Error");
}
}