87,922
社区成员
发帖
与我相关
我的任务
分享
//添加原型函数
String.prototype.replaceall=function(){
return this.replace(/( |\\)/g,function($1){if($1==" ")return "%20";else if($1=="\\")return "/";});
}
var v="c:\\my pic\\0.jpg";
alert(v);
v=v.replaceall();//使用原型更改值
alert(v);
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312"/>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="gotopage.php">
<input type="file" name="userfile" onchange="SetViewPic(this)"/><br/>
</form>
预览图片<br>
</body></html>
<script>
var v="c:\\my pic\\0.jpg";
v=v.replace(/ /g,"%20");
alert(v);
v=v.replace(/\\/g,"/");
alert(v)
//暂时测试ie6和safari【3.0.4】能使用img标签的file:///协议浏览图片
//其他只能交由服务器处理了,并且容器为iframe标签
var UseIMGTag=navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1;
if(!UseIMGTag&&document.all){//不是safari,则判断下是否为ie7以下的浏览器
var m=/msie(.+?)(?=;)/i.exec(navigator.userAgent);
if(m&&parseInt(m[1])<7)UseIMGTag=true;
}
var Reg=/\.(gif|jpg|bmp|png)$/i
if(UseIMGTag)document.write("<img id='imgView'/>");
else document.write('<iframe src="javascript:void(0)" frameborder="1" '
+'style="width:100px;height:100px;" id="ifViewPic" name="ifViewPic"></iframe>');
function $(id){return document.getElementById(id);}
function SetViewPic(obj)
{
//先检查是否为图片
if(!Reg.test(obj.value)){
alert("不是图片文件!");
return;
}
if(UseIMGTag)$("imgView").src="file:///"+obj.value;//可以使用file:///协议的
else{//提交到viewpic.php,然后输出临时文件
var f=obj.form;
var oldAC=f.action;
var oldTag=f.target;
f.target="ifViewPic";
f.action="viewpic.php";
f.submit();
//改回默认值
f.target=oldTag;
f.action=oldAC;
}
}
</script>
<?php
$type=$_FILES['userfile']['type'];
if(!preg_match('/(jpeg|jpg|png|gif|bmp)$/',$type))die("{$type}为不可以浏览器的类型!");
$tmpPath=$_FILES['userfile']['tmp_name'];
if(is_uploaded_file($tmpPath)){
header("content-type:$type");
readfile($_FILES['userfile']['tmp_name']);
}
else die("上传失败无法预览!");
?>
var v="c:\\my pic\\0.jpg";
v=v.replace(/ /g,"%20");
alert(v);
v=v.replace(/\\/g,"/");
alert(v)
<input type="file" onchange="SetViewPic(this.value)"/><br/>
<img id="imgView"/>
<script>
function SetViewPic(v)
{//ie6和ff 2.0.0.12测试通过
document.getElementById("imgView").src="file:///"+v;
}
</script>
var path1 = "c:\\dir1\\dir2";
alert(path1);
alert(path1.replace(/\\/g,"/"));
var _s = document.getElementById(soundName);
var url = document.URL
url=url.replace(/(^.*?\/\/.*?\/.*?\/).*?$/,"$1")+"tam/abc/sound/"+soundName+".wav"
alert(url)
_s.src = url;