想用javacript打开本地网页,但路径中有中文就不行了,如何解决?

hlidea 2007-01-30 02:47:29
<!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函数转换也不行,如何做才能用上述代码打开包含中文路径的文件?
...全文
231 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengmingjie 2007-01-30
  • 打赏
  • 举报
回复
不知道在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>
hlidea 2007-01-30
  • 打赏
  • 举报
回复
没人懂吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧