阅读:6441回复:3
nsIFile (nsIFilePicker?) 中文档名显示 "_"
我电脑运行环境:
英文版winXP sp1, 英文版firefox 1.5.0.7 以下的程序是让人点击open按钮,然后选者文件。整个xul页面只有一个按钮和一个listbox. <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <!DOCTYPE window> <window xmlns= "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script> function openFile() { var nsIFilePicker = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker); fp.init(window, "Select a File", nsIFilePicker.modeOpenMultiple); var res = fp.show(); if (res == nsIFilePicker.returnOK){ var fileEnum = fp.files; var item = null; var file = null; while(fileEnum.hasMoreElements()) { item = fileEnum.getNext(); file = item.QueryInterface(Components.interfaces.nsIFile); // Fill in information into listbox var cellName = document.createElement("listcell"); cellName.setAttribute("label", file.leafName ); var cellSize = document.createElement("listcell"); cellSize.setAttribute("label", file.fileSize); var cellType = document.createElement("listcell"); cellType.setAttribute("label", "test文件おの태으"); var cellModified = document.createElement("listcell"); cellModified.setAttribute("label", file.lastModifiedTime); var item = document.createElement("listitem"); item.appendChild(cellName); item.appendChild(cellSize); item.appendChild(cellType); item.appendChild(cellModified); var list = document.getElementById("fileList"); list.appendChild(item); } } } </script> <hbox> <button label="Open..." oncommand="openFile()" /> </hbox> <listbox id="fileList" seltype="multiple" flex="1"> <listhead> <listheader label="Name"/> <listheader label="Size"/> <listheader label="Type"/> <listheader label="Modified" /> </listhead> <listcols > <listcol flex="1"/> <listcol flex="1"/> <listcol flex="1"/> <listcol flex="1"/> </listcols> </listbox> </window> 假如我只选取英文文档名,文件名就会完全无误地显示在listbox。cellType栏位只是用来确定xul可以显示中文。 假如我选取带有中文名的文档,listbox就不会显示了。检查javascript console: Error: uncaught exception: [Exception... "Component returned failure code: 0x80520012 (NS_ERROR_FILE_NOT_FOUND) [nsIFile.fileSize]" nsresult: "0x80520012 (NS_ERROR_FILE_NOT_FOUND)" location: "JS frame :: chrome://findfile/content/test.xul :: openFile :: line 28" data: no] 所以我把以下几行的程式comment out. var cellSize = document.createElement("listcell"); // cellSize.setAttribute("label", file.fileSize); var cellModified = document.createElement("listcell"); // cellModified.setAttribute("label", file.lastModifiedTime); 现在listbox可以显示文件名,但所有中文字都一律换成"_". 例如:tall文.txt 成了tall_.txt 我不知道这问题是因为nsIFilePicker或nsIFile.如何解决这问题呢?谢谢。 |
|
1楼#
发布于:2006-11-08 17:20
如果楼主用的编辑器是UEstudio,可以试试
文件->转换->ASCII->UTF-8 试试看呢 |
|
2楼#
发布于:2006-11-08 17:20
试试用nsIScriptableUnicodeConverter先转成unicode
|
|
3楼#
发布于:2006-11-08 17:20
XUL文件编码问题,改一下第一行就OK了。
我现在也正在作XUL程序,也遇到了很大的困难。 如果可以的话,楼上留个QQ可以吗? |
|