87,918
社区成员
发帖
与我相关
我的任务
分享
<script>
function getValueIE8(id){
var ip = document.getElementById(id);
ip.select();
return document.selection.createRange().text;
}
function Preview()
{
var imgSrc = getValueIE8('FileUpload1');
document.getElementById("MyPreview").src = imgSrc;
}
</script>
<input type="file" ID="FileUpload1" runat="server" onchange="Preview(this.value);" Width="270" />
<img ID="MyPreview" runat="server" />
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<input type="file" id="t" onchange="getValue()" />
<div id="t2" style="visibility:hidden;"></div>
<img id="t1"/>
<script type="text/javascript">
var isIE = (document.all) ? true : false;
var isIE7 = isIE && (navigator.userAgent.indexOf('MSIE 7.0') != -1);
var isIE8 = isIE && (navigator.userAgent.indexOf('MSIE 8.0') != -1);
var isMozilla = /mozilla/.test( navigator.userAgent ) && !/(compatible|webkit)/.test( navigator.userAgent )
var isOpera = /Opera/.test( navigator.userAgent )
var t1=document.getElementById("t1")
var t2=document.getElementById("t2")
if(isIE7 || isIE8){
t1.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image')"
t2.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale')"
}
function getValue(){
var t=document.getElementById("t");
var value=t.value
if(isIE7 || isIE8){
t.select();
var value=document.selection.createRange().text;
document.selection.empty();
}else if(isMozilla){
value = t.files[0].getAsDataURL();
}
alert(value)
if(isIE7 || isIE8){
t1.style.visibility="hidden"
t2.style.visibility=""
t1.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=value;
t2.style.width=t1.offsetWidth
t2.style.height=t1.offsetHeight
t2.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=value;
}else{
t1.src=value
}
}
</script>
</body>
</html>
function getValueIE8(id){
var ip = document.getElementById(id);
ip.select();
return document.selection.createRange().text;
}
function Preview()
{
var imgSrc = getValueIE8('FileUpload1');
document.getElementById("MyPreview").src = imgSrc;
}
function getValueIE8(id){
var ip = document.getElementById(id);
ip.select();
return document.selection.createRange().text;
}
function Preview()
{
var imgSrc = getValueIE8(FileUpload1);
document.getElementById("MyPreview").src = imgSrc;
}
<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html>
<head>
<title>File Input</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
body{font-size:13px;}
.box{border:1px solid #eee;background:#ffe;padding:10px 30px;margin:10px;}
dt{font-weight:bold;}
dt,img,textarea{margin:8px 0;}
em{color:#f00; font-style:normal;}
span{color:#999;}
.ft{color:#999;font-size:11px;text-align:right}
</style>
</head>
<body>
<h2>在不同浏览器中获取File Input的value</h2>
<div class="box">
<input type="file" name="foo" id="foo" size="60" />
<input type="button" value="Show Value" onclick="alert(document.getElementById('foo').value)" />
<input type="button" value="Show Value in FF3" onclick="alert(getValueFF('foo'))" />
<input type="button" value="Show Value in IE8" onclick="alert(getValueIE8('foo'))" />
</div>
<div class="box">
<dl>
<dt>Firefox 2.x、IE6、IE7</dt>
<dd>在本地及远端都可以取得完整地址(包括路径及文件名)</dd>
</dl>
<dl>
<dt>Firefox 3.0</dt>
<dd>在本地及远端都<em>不能</em>取得完整地址,只能获得文件名</dd>
</dl>
<dl>
<dt>IE8、IE7[IE8模拟]</dt>
<dd>在本地可以取得完整地址(包括路径及文件名),在远端<em>不能</em>取得完整地址,只能获得文件名</dd>
</dl>
<span>* 本地:localhost</span>
</div>
<div class="box">
<h3>如何在Firefox3下取得完整路径</h3>
<p>
第一步:打开"about:config"页面,查找"signed.applets.codebase_principal_support"属性,将其值设置为true。
</p>
<p>
第二步:在javascript中采用以下代码进行获取:<br />
<textarea style="width:800px;height:300px;">
function getValueFF(id){
var ip = document.getElementById(id);
if (ip.files) {
//ffx3 - try to have access to full path
try {
netscape.security.PrivilegeManager.enablePrivilege( 'UniversalFileRead' )
}
catch (err) {
//need to set signed.applets.codebase_principal_support to true
}
};
return ip.value;
}
</textarea>
</p>
<p>
经过以上步骤,在Firefox3下获取file input的value时,会弹出一个Internet Security的对话框,点击"Allow"将可以获取完整value。<br />
<span>资料:<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=143220" target="_blank">https://bugzilla.mozilla.org/show_bug.cgi?id=143220</a></span>
</p>
</div>
<div class="box">
<h3>如何在IE8下取得完整路径</h3>
<p>方法一:使用selection.createRange <br />
<textarea style="width:800px;height:150px;">
function getValueIE8(id){
var ip = document.getElementById(id);
ip.select();
return document.selection.createRange().text;
}
</textarea>
</p>
<p>方法二:引用:<a href="https://bugzilla.mozilla.org/attachment.cgi?id=328849" target="_blank">https://bugzilla.mozilla.org/attachment.cgi?id=328849</a></p>
</div>
<script language="JavaScript">
function getValueFF(id){
var ip = document.getElementById(id);
if (ip.files) {
//ffx3 - try to have access to full path
try {
netscape.security.PrivilegeManager.enablePrivilege( 'UniversalFileRead' )
}
catch (err) {
//need to set signed.applets.codebase_principal_support to true
}
};
return ip.value;
}
function getValueIE8(id){
var ip = document.getElementById(id);
ip.select();
return document.selection.createRange().text;
}
</script>
</body>
</html>