如何使用 Blob 在文本文件中添加换行符
How to add line breaks in a text file using Blob
在我的项目中,我需要能够下载包含一些变量的文本文件,但我希望每行都有一个声明,如下所示:
但我不知道如何添加换行符。现在,我只是像这样来,一切都在一条线上:
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
这是我遇到问题的部分代码,你能帮我解决这个问题吗?
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)
我相信你 escape character 错了。您正在使用正斜杠 /n
,但换行符的转义序列使用反斜杠:\n
。尝试使用此行替换代码中的 var body
行:
var body = listPointStringified+"\n"+globalTotalCaisses+"\n"+globalNombreCouches+"\n"+globalCaissesParCouches+"\n"+globalIntercalaires+"\n"+globalCaisseH+"\n"+globalOffsetAppDeposeZ
在我的项目中,我需要能够下载包含一些变量的文本文件,但我希望每行都有一个声明,如下所示:
但我不知道如何添加换行符。现在,我只是像这样来,一切都在一条线上:
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
这是我遇到问题的部分代码,你能帮我解决这个问题吗?
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)
我相信你 escape character 错了。您正在使用正斜杠 /n
,但换行符的转义序列使用反斜杠:\n
。尝试使用此行替换代码中的 var body
行:
var body = listPointStringified+"\n"+globalTotalCaisses+"\n"+globalNombreCouches+"\n"+globalCaissesParCouches+"\n"+globalIntercalaires+"\n"+globalCaisseH+"\n"+globalOffsetAppDeposeZ