Rangeerror: Invalid string length using JSZIP

Asked

Viewed 53 times

0

The functionality of my application is to upload zip files, inside each zip file has files XML, in the process it has to unzip the file and then it saves the data in the database, the application managed to upload small files with 150 megas, but can not upload files with 4 Gigas, the message to give is the memory overflow;

inserir a descrição da imagem aqui

Take a look here too;

inserir a descrição da imagem aqui

For what I searched on the internet in the javascript algorithm would need to be able to divide the process not to overload the application, know the solution is easy, difficult and put into practice, take a look at the javascript algorithm that uploads zip files.

async function handleFile(data, selecao) {
        var f = data.files[0];
        var valido = true;

        var r  = new Array();
        r ["Cargo.xml"] = 0,
        r ["ClasseNivelFaixa.xml"] = 0,
        r ["CodigoVantagemDesconto.xml"] = 0,
        r ["Dependente.xml"] = 0,
        r ["FolhaPagamento.xml"] = 0,
        r ["HistoricoFuncional.xml"] = 0,
        r ["Lotacao.xml"] = 0,
        r ["Servidor.xml"] = 0,
        r ["VantagemDesconto.xml"] = 0,
        r ["Vinculo.xml"]  = 0;
        r ["undefined"]  = 0;

        var dateBefore = new Date();
        var jsZipInstance = new JSZip();

        await jsZipInstance.loadAsync(f)                                   // 1) read the Blob
        .then(function(zip) {
            var dateAfter = new Date();

                zip.forEach(function (relativePath, zipEntry) {  // 2) print entries
                    if (typeof r[zipEntry.name] !== 'undefined'){
                        r [zipEntry.name] = r [zipEntry.name] + 1;          
                    }else{
                        r ["undefined"]  = r ["undefined"] + 1;
                    }

                });

                var conteudoIncompativel = false || r ["undefined"] > 0;

                if (!conteudoIncompativel){
                    for (x in r){
                        if (r[x] != 1 && x != "undefined" ){
                            conteudoIncompativel = true;
                            break;  
                        } 
                    }
                }

                if (conteudoIncompativel){
                    var mensagem = "${codigoMensagem.get('1003')}";
                    insereBotoes(mensagem, new Date());
                    $("#myModal .modal-title").text("Gerar PDF com erros encontrados");
                    $("#myModal .modal-body").html(mensagem);
                    $("#myModal").modal("show");    
                    valido = false; 
                }

        }, function (e) {
            insereBotoes("${codigoMensagem.get('1004')}", new Date());
            $("#myModal .modal-title").text("Gerar PDF com erros encontrados");
            $("#myModal .modal-body").text("${codigoMensagem.get('1004')}");
            $("#myModal").modal("show");
            valido = false;
        });

        var teste = "";
        var quantidadeVazio = 0;

        if (valido){
            for (x in r){


//              try {
                    if (x != "undefined"){//uint8array, arraybuffer
                        await jsZipInstance.file(x).async("string").then(function (data) {

                            console.log(data);

                          var itemComp = x == "FolhaPagamento.xml" ? "ItemFolha" : "Item" + x.substr(0, x.length - 4);  
                          if (data.indexOf(itemComp) == -1){
                            quantidadeVazio ++; 
                          }

                          if (data.length == 0){
                              valido = false;
                              teste = x;
                          }

                        });

                        if (teste.length > 0){
                            break;
                        }
                    }
//              }catch(err) {
//                  console.log(err.message)
//              }

            }

            if (teste.length > 0){
                insereBotoes("${codigoMensagem.get('1009')}".replace("{0}", teste), new Date());
                $("#myModal .modal-title").text("Gerar PDF com erros encontrados");
                $("#myModal .modal-body").text("${codigoMensagem.get('1009')}".replace("{0}", teste));
                $("#myModal").modal("show");
            } else if (quantidadeVazio == 10){
                insereBotoes("${codigoMensagem.get('1011')}".replace("{0}", teste), new Date());
                $("#myModal .modal-title").text("Gerar PDF com erros encontrados");
                $("#myModal .modal-body").text("${codigoMensagem.get('1011')}");
                $("#myModal").modal("show");
                valido = false;
            }
        }    
       return valido;       
    }       

Would anyone have an idea of how to refactor the code so it splits the file upload process?

  • You. used wrong method. var r = new Array() r variable only accepts array and no object.

  • All right Maury, if you think that’s the problem, please how could I do?

  • It is useless to use new Array() why not use {} or []. That less code your script.

  • All right @Maurydeveloper I already understood that you had informed me that it was not right, please you would have idea how I could modify my code or you mean that my algorithm is all wrong because of this?

  • Only change variable r to var r = [];. You could send me the dependency links (jQuery,JSZIP).

  • With your suggestion generated an error, and prevented from 3-megas files get processed, which before.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.