读书人

想用javacript打开本地网页,但路径中有

发布时间: 2011-11-23 23:51:20 作者: rapoo

想用javacript打开本地网页,但路径中有中文就不行了,如何解决?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> Untitled Page </title>
<script language= "javascript " type= "text/javascript ">
// <!CDATA[


function qswhEncodeURI(str)
{

var m= " ",sp= "! '()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ "
for(var i=0;i <str.length;i++)
{
if(sp.indexOf(str.charAt(i))!=-1)
{
m+=str.charAt(i)
}else
{
var n=str.charCodeAt(i)
var t= "0 "+n.toString(8)
if(n> 0x7ff)
m+=( "% "+(224+parseInt(t.slice(-6,-4),8)).toString(16)+ "% "+(128+parseInt(t.slice(-4,-2),8)).toString(16)+ "% "+(128+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n> 0x7f)
m+=( "% "+(192+parseInt(t.slice(-4,-2),8)).toString(16)+ "% "+(128+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n> 0x3f)
m+=( "% "+(64+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n> 0xf)
m+=( "% "+n.toString(16)).toUpperCase()
else
m+=( "% "+ "0 "+n.toString(16)).toUpperCase()


}
}
return m;
}


function Button1_onclick() {
var loc= "file:/ " + qswhEncodeURI(document.getElementById( "File1 ").value);
var code= 'location= " '+loc+ ' " ';
eval(code);

}

// ]]>
</script>
</head>
<body>
please choose the file you want to open <br />
  
<input id= "File1 " type= "file " />
<input id= "Button1 " type= "button " value= "button " onclick= "return Button1_onclick() " />

</body>
</html>

感觉问题出在escape转换上,反正只要路径中包含中文就打不开,我用javascript的escape函数转换也不行,如何做才能用上述代码打开包含中文路径的文件?

[解决办法]
不知道在web服务器上加一个字符过滤的配置可不可以,你可以试试
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> common.util.filter.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>

读书人网 >Java Web开发

热点推荐