As valid that when an excel does not contain anything, do not download it by javascript?

0

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");
        }
    }
    
asked by Alcides Salazar 29.09.2017 в 14:45
source

1 answer

1

Instead of returning null in the controller, try to save a script where you indicate that it is empty

byte[] fileBytes = _IAdi.ExportExcel(cod); 
if (fileBytes == null) 
     return Content("<script>alert("Excel nulo !");</script>");;
    
answered by 29.09.2017 / 17:03
source