如何使用 javascript 动态创建和下载 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5143504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create and download an XML file on the fly using javascript?
提问by Rahul
I got a requirement as following:
我有一个要求如下:
There is a link on a web page. As user clicks on link it should create a file on the fly and a download box pops up. How to do it using java script?
网页上有一个链接。当用户点击链接时,它应该会即时创建一个文件并弹出一个下载框。如何使用java脚本来做到这一点?
采纳答案by rmflow
If the user trusts you, you can can create XML file directly in his filesystem. Example code for Mozilla Firefox:
如果用户信任你,你可以直接在他的文件系统中创建 XML 文件。Mozilla Firefox 的示例代码:
function mozillaSaveFile(filePath,content)
{
if(window.Components) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
if(!file.exists())
file.create(0,0664);
var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
out.init(file,0x20|0x02,00004,null);
out.write(content,content.length);
out.flush();
out.close();
return true;
} catch(ex) {
return false;
}
}
return null;
}
if you need support for all browsers, see how it is implemented in http://www.tiddlywiki.com
如果您需要对所有浏览器的支持,请在http://www.tiddlywiki.com 中查看它是如何实现的
EDIT:This doesn't work for Firefox 17+because changing privileges was deemed unsafe and removed. see here for more details: https://bugzilla.mozilla.org/show_bug.cgi?id=546848#c57
编辑:这不适用于Firefox 17+,因为更改权限被视为不安全并已删除。有关更多详细信息,请参见此处:https: //bugzilla.mozilla.org/show_bug.cgi?id=546848#c57
回答by Juan
You can use blobs as shown in this example http://html5-demos.appspot.com/static/a.download.html
您可以使用此示例中所示的 blob http://html5-demos.appspot.com/static/a.download.html
You can have a javacript function with the following code
您可以使用以下代码获得 javacript 函数
var xmltext = "<sometag><someothertag></someothertag></sometag>";
var pom = document.createElement('a');
var filename = "file.xml";
var pom = document.createElement('a');
var bb = new Blob([xmltext], {type: 'text/plain'});
pom.setAttribute('href', window.URL.createObjectURL(bb));
pom.setAttribute('download', filename);
pom.dataset.downloadurl = ['text/plain', pom.download, pom.href].join(':');
pom.draggable = true;
pom.classList.add('dragout');
pom.click();
回答by Mikel
After try what Andreas said I will add something:
在尝试 Andreas 所说的之后,我将添加一些内容:
Script:
脚本:
function createAndOpenFile(){
var stupidExample = '<?xml version="1.0" encoding="utf-8"?><aTag>something</aTag>';
document.open('data:Application/octet-stream,' + encodeURIComponent(stupidExample));
}
You have a link like this, note the new download atribute, with it you put the file name.
你有一个这样的链接,注意新的下载属性,你把文件名放在里面。
<a href="#" onclick="createAndOpenFile()" download="file.xml">Donwload</a>
It works at least in Chrome 27 and Firefox 21.
它至少适用于 Chrome 27 和 Firefox 21。
Improved are welcome :-)
欢迎改进:-)
回答by Andreas Gohr
You could create a data-URI. Most modern browsers should be able to understand it. See http://en.wikipedia.org/wiki/Data_URI_scheme
您可以创建一个数据 URI。大多数现代浏览器应该能够理解它。请参阅http://en.wikipedia.org/wiki/Data_URI_scheme
回答by Omar BOUFTINI
decodeRequest(textToDecode) {
var decodedString = atob(textToDecode);
var fileName = "fileName1"+'_RQ';
var fileType = '.xml';
var blob = new Blob([decodedString], { type: fileType });
var a = document.createElement('a');
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, 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);
}