效果如下:
實現(xiàn)代碼:
JS腳本如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> <script language="javascript" type="text/javascript">
function getFullPath(obj)
{
if(obj)
{
//ie
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
obj.select();
return document.selection.createRange().text;
}
//firefox
else if(window.navigator.userAgent.indexOf("Firefox")>=1)
{
if(obj.files)
{
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}
function yulan()
{
var filePath =getFullPath(document.getElementById('UpFile'));
var fileText =filePath.substring(filePath.lastIndexOf("."),filePath.length);
var fileName =fileText.toLowerCase();
if ((fileName!='.jpg')&&(fileName!='.gif')&&(fileName!='.jpeg')&&(fileName!='.png')&&(fileName!='.bmp'))
{
alert("對不起,系統(tǒng)僅支持標(biāo)準(zhǔn)格式的照片,請您調(diào)整格式后重新上傳,謝謝 !");
document.form1.UpFile.focus();
}
else
{
document.getElementById("preview").innerHTML="<img src='"+getFullPath(document.getElementById('UpFile'))+"' width=120 style='border:6px double #ccc'>";
}
}
</script>
ASPX頁面代碼:
<input type="file" id="UpFile" class="stylebtn" name="UpFile" onchange="yulan()" />;
<asp:Button ID="btnUpload" runat="server" Text="上 傳"
onclick="btnUpload_Click" Height="22px" CssClass="stylebtn"/>
<div id="preview"></div>
在IE7下測試正常 !