所以我得到了這個代碼:
<input type='button' value='clickme' onclick='download(\"" . $rowtable['Protocolo'] . ".xml\",\"" . $rowtable['XML']. "\");'/></input>
<script>
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
</script>
它從我的資料庫中得到 2 個結果,即一個 String(Protocolo) 和一個 TEXT(XML)。但問題是我的 TEXT(XML) 資料如下所示:
<?xml version="1.0" encoding="UTF-8"?><enviNFe versao="4.00" xmlns="asdasdasdasdasd"><idLote>1</idLote><indSinc>1</indSinc><NFe xmlns="asdsdasadsad">
正如你所看到的,它有很多“。它有很多錯誤,代碼。所以我的問題是,我怎樣才能將所有這些 $rowtable['XML'] 轉換成一個正確的字串。所以我可以將它傳遞給JS,所以我可以下載它。
如果您有任何更好的方法從我的資料庫下載此 XML 資料,也歡迎。
uj5u.com熱心網友回復:
您可以使用 將 xml 編碼到 php 中urlencode,并直接在 javascript 中使用它的值。
<input type='button' value='clickme' onclick='download(\"" . $rowtable['Protocolo'] . ".xml\",\"" . urlencode($rowtable['XML']). "\");'/></input>
<script>
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' text);
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
</script>
uj5u.com熱心網友回復:
您在按鈕input屬性中缺少對 Javascript 和 HTML 的轉義。使用urlencode()or base64_encode()(在 PHP 中)會對特殊字符進行編碼,這樣它們就不會破壞 JS 或 HTML 代碼。
但是,如果您使用a元素,則無需 JavaScript 即可完成此操作。
<html>
<body>
<?php
$data = '<foo>Example String</foo>';
$fileName = 'demo.xml';
?>
<a
class="DownloadButton"
href="data:text/plain;charset=utf8,<?=urlencode($data)?>"
download="<?=htmlspecialchars($fileName)?>">Download</a>
</body>
</html>
資料被編碼并放入資料 URL 中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321709.html
標籤:javascript php xml 细绳 下载
上一篇:是否可以在android中以編程方式創建string.xml檔案?
下一篇:從包含XML資料的特定列創建表
