How to add line breaks in a text file using Blob

Viewed 209

In my project, I need to be able to download a text file containing some variables but I want to have a declaration per line, like this:

enter image description here

but I don't know how to add line breaks. For now, I am just getting cometing like this, with everything on one line:

listPoints = [posx(-306,618.5,-275,0,180,0),posx(-308,1028.5,-275,0,180,0),posx(308,618.5,-275,0,180,0),posx(308,1030.5,-275,0,180,0)]/nGlobal_total_caisses = len(listPoints)/nGlobal_nombre_couches = 1/nGlobal_caisses_par_couches = [4]/nGlobal_intercalaires = []/nGlobal_caisse_h = 425/nGlobal_offset_app_depose_z = Global_caisse_h + 50

Here is the part of my code I'm struggling with, could you help me solve this problem?

            var listPointStringified = "listPoints = "+JSON.stringify(listPoints).replace(/"/g,'')
            var globalTotalCaisses = "Global_total_caisses = len(listPoints)"
            var globalNombreCouches = "Global_nombre_couches = "+ JSON.stringify(this.layersOnThePal.length).replace(/"/g,'')
            var globalCaissesParCouches = "Global_caisses_par_couches = " + JSON.stringify(nombreBoiteParCouches).replace(/"/g,'')
            var globalIntercalaires = "Global_intercalaires = []"
            var globalCaisseH = "Global_caisse_h = "+ this.caisse.height
            var globalOffsetAppDeposeZ = "Global_offset_app_depose_z = Global_caisse_h + 50"
    
            var body = listPointStringified+"/n"+globalTotalCaisses+"/n"+globalNombreCouches+"/n"+globalCaissesParCouches+"/n"+globalIntercalaires+"/n"+globalCaisseH+"/n"+globalOffsetAppDeposeZ
            
    
            var blob = new Blob([body], {type: 'text/plain', endings:'native'})
                var a = document.createElement('a');
                a.download = this.name
                a.href = URL.createObjectURL(blob);
                a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');
                a.style.display = "none";
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                setTimeout(function() { URL.revokeObjectURL(a.href); },1500)
1 Answers

I believe you have the escape character wrong. You are using a forward slash /n, but the escape sequence for newline uses a backslash: \n. Try this line to replace the var body line in your code:

var body = listPointStringified+"\n"+globalTotalCaisses+"\n"+globalNombreCouches+"\n"+globalCaissesParCouches+"\n"+globalIntercalaires+"\n"+globalCaisseH+"\n"+globalOffsetAppDeposeZ

Related