0

In my application (developed with AngularJS framework) I must create and download an excel file in client side (Javascript language). To do this, I have this code

            var form = document.createElement('form');
            form.action = 'download';
            form.method = 'POST';
            form.style.display = 'none';

            var inputsrtoken = document.createElement('input');
            inputsrtoken.style.setAttribute("width", "0");
            inputsrtoken.style.setAttribute("diplay", "none");
            inputsrtoken.style.setAttribute("visibility", "hidden");                                
            inputsrtoken.type = 'text';
            inputsrtoken.name = 'srtoken';
            inputsrtoken.value = token;

            var inputData = document.createElement('input');
            inputData.style.setAttribute("width", "0");
            inputData.style.setAttribute("diplay", "none");
            inputData.style.setAttribute("visibility", "hidden");                               
            inputData.type = 'text';
            inputData.name = 'inputRicalcolo';
            inputData.value = JSON.stringify(excelData);

            var submit = document.createElement('input');
            submit.type = 'submit';
            submit.id = 'submitProject';

            form.appendChild(inputsrtoken);
            form.appendChild(inputData);
            form.appendChild(submit);
            document.body.appendChild(form);

            $('#submitProject').click();
            document.body.removeChild(form);

Where in variable excelData are included some usefull information for the generation of excel file.

Server side, I have this code (in the download servlet)

           if (Base64.isArrayByteBase64(excelin.getBytes("UTF-8"))) {
                excelin = new String(Base64.decodeBase64(excelin.getBytes("UTF-8")), "UTF-8");
            }

            bpdf = createExcelRicalcolo(excelin);

            String chiave = request.getParameter("chiave");
            DateFormat formatter = new SimpleDateFormat("ddMMyyyy-HHmm");
            String format = formatter.format(new Date());
            String filename = "RICALCOLO_ONLINE_"+chiave +"_"+ format + ".xls";

            byte[] b = bpdf;

            if (b != null) {
                int length = b.length;
                ServletOutputStream op = resp.getOutputStream();
                resp.setContentType("application/ms-excel");
                resp.setContentLength(length);
                resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
                resp.setHeader("Expires", "0");
                resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                resp.setHeader("Pragma", "public");

                op.write(b); 
                op.flush();
                op.close();
            }

It works great!
But now, this is my question: is it possible to generate the file, without downloading it, but only to save it (maybe with a service in javascript) for a subsequent download request?
In fact, my goal is the following: I would like to generate an excel file (and not show to the user), save it on a DB2 column as a CLOB and only then (by pressing for example another button on the app) give the user the possibility to download it.
Do you believe it is possible to do such a thing without upsetting the logic of the code?
Saving the file as a CLOB will be the following problem to be framed.
Grace to anyone who will try to help me.

Raul
  • 115
  • 5
  • 16
  • You can perform an HTTP async call and store the stream as a **Blob** https://developer.mozilla.org/en-US/docs/Web/API/Blob. Sounds like you need to perform an **async** http request, so I would suggest you to take a look at this https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – briosheje Feb 08 '19 at 10:49
  • Should I store the stream as a blob (why not a clob?) before go to server side? – Raul Feb 08 '19 at 11:19

0 Answers0