Javascript 下载时,哈希标记字符使文件不完整
我有一个文本文件,在用户下载之前我正在对它做一些更改。所有更改都是使用Javascript/Typescript进行的,不会产生任何错误。我面临的问题是,当用户下载文件时,它总是在一个特定的和不相关的单词之后出现不完整。如果在实际下载之前我Javascript 下载时,哈希标记字符使文件不完整,javascript,typescript,Javascript,Typescript,我有一个文本文件,在用户下载之前我正在对它做一些更改。所有更改都是使用Javascript/Typescript进行的,不会产生任何错误。我面临的问题是,当用户下载文件时,它总是在一个特定的和不相关的单词之后出现不完整。如果在实际下载之前我console.log,我可以很好地看到该文件。问题的根源似乎是添加了对该文件的引用,因为如果我删除此“添加引用”部分,该文件将按预期下载。遗憾的是,我无法删除此部分 此功能用于用户在浏览器中导航时: myDownloadFunction(file: Feat
console.log
,我可以很好地看到该文件。问题的根源似乎是添加了对该文件的引用,因为如果我删除此“添加引用”部分,该文件将按预期下载。遗憾的是,我无法删除此部分
此功能用于用户在浏览器中导航时:
myDownloadFunction(file: Features[]) {
...
// Features is OpenLayer's Features
// https://openlayers.org/en/latest/apidoc/module-ol_Feature-Feature.html
// Declare variables and minor changes
let final_output:string = kml_format.writeFeatures(file);
...
// Add references
for (let feature of this.featuresToExport) {
let idToExport = feature.id_;
let featureColor:string = feature.values_.color;
let featureHexColor = this.getColorByName(featureColor);
let colorElement = '<Style id="app_style_'+idToExport+'"><IconStyle><Icon><href>https://earth.google.com/earth/rpc/cc/icon?color='+featureHexColor+'&id=2000&scale=4</href></Icon></IconStyle></Style>';
// Add style element
let indexOfDocument = final_output.indexOf("Document");
let indexOfClosingDocument = final_output.indexOf(">", indexOfDocument) + 1;
let output = [
final_output.slice(0, indexOfClosingDocument),
colorElement,
final_output.slice(indexOfClosingDocument)
].join('');
// Add reference to style element
let indexOfPlacemark = output.indexOf('Placemark id="' + idToExport + '"');
let indexOfClosingPlacemark = output.indexOf(">", indexOfPlacemark) + 1;
output = [
output.slice(0, indexOfClosingPlacemark),
'<styleUrl>#app_style_'+idToExport+'</styleUrl>',
output.slice(indexOfClosingPlacemark)
].join('');
final_output = output;
}
this.mainDoc = "data:text/json;charset=utf-8," + final_output;
console.log(this.mainDoc); // <-- Here I can see the whole document perfectly fine
let link = document.createElement("a");
link.download = this.file_name + this.file_extension;
link.href = this.mainDoc;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
link = null;
}
您可以看到一个示例,说明文件的假定方式和下载方式
我尝试了其他一些方法,我想我已经缩小了问题的根源。当我从引用文本中删除hashtag字符(#)时,一切正常。如果我留下标签,它就会断开。有人知道为什么会这样吗?我像往常一样试图逃跑,但没有成功
let referenceElement = '<styleUrl>#app_style_'+idToExport+'</styleUrl>'; // It will break
let referenceElement = '<styleUrl>app_style_'+idToExport+'</styleUrl>'; // Working fine
let referenceElement='#app_style.'+idToExport+'''//它会碎的
let referenceElement='app_style_u'+idToExport+''';//工作正常
通过对hashtag字符使用解决了该问题:
let referenceElement = '<styleUrl>%23app_style_'+idToExport+'</styleUrl>';
let referenceElement='%23app\u style\u'+idToExport+'';
通过对hashtag字符使用解决了该问题:
let referenceElement = '<styleUrl>%23app_style_'+idToExport+'</styleUrl>';
let referenceElement='%23app\u style\u'+idToExport+'';
最终输出的确切内容是什么?我看不到它在代码中的定义。final\u output
是正在编辑的主文档。在显示的代码中有一个字符串类型的变量。您的意思是,它在您未发布的代码中声明为字符串?在发布的代码中,它没有声明。没有let/const/var final\u output
修复了这个问题,并添加了另一个我尝试过的东西。什么是final\u output
?我看不到它在代码中的定义。final\u output
是正在编辑的主文档。在显示的代码中有一个字符串类型的变量。您的意思是,它在您未发布的代码中声明为字符串?在发布的代码中,它没有声明。没有let/const/var final_output
修复了这个问题,并添加了另一个我尝试过的东西。