En primer lugar, permito que los usuarios carguen varios archivos. Luego uso bucle con fileReader para leer todos los archivos cargados. Luego edito algunos datos de cada archivo y creo nuevos archivos que contienen cada dato. Finalmente, quiero descargar todos estos archivos usando la biblioteca fileSaver. El problema es que solo puedo descargar el último archivo con el siguiente código. Estaría muy agradecido si alguien puede arreglarlo. Código:
<html> <body> <!-- Link to FileSaver Library --> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js" integrity="sha512-Qlv6VSKh1gDKGoJbnyA5RMXYcvnpIqhO++MhIM2fStMcGT9i2T//tSwYFlcyoRRDcDZ+TYHpH8azBBCyhpSeqw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <!-- Allow users to upload multiple files --> <input type="file" id="real-file" accept=".xml" multiple/> <script> const realFileBtn = document.getElementById("real-file") realFileBtn.addEventListener("change", function() { if (realFileBtn.value) { // Use loop to do for all uploaded files for(var i = 0; i < realFileBtn.files.length; i++){ var fr = new FileReader() var fileName = realFileBtn.files[i].name fr.readAsText(realFileBtn.files[i]) fr.onload = function() { // After loading the file, I get the data of it and edit some information (for example: replace "a" with "b") var data = fr.result.replaceAll("a", "b") // I create a new file that contain the edited data with the same name as the uploaded file var myFile = new File( [data], fileName, {type: "text/xml; charset = utf8"}) // Then I download that file saveAs(myFile) // The issue is that I can only download 1 file and that file is the last file you uploaded // I want to read the first file, edit its data and download the file contain that data. Then do the same with second file, // third file,... n file } } } }) </script> </body>