讀取input:file的路徑并顯示本地圖片的方法
<!doctype html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Image preview example</title>
<script type="text/javascript">
var loadImageFile = (function () {
if (window.FileReader) {
var oPreviewImg = null, oFReader = new window.FileReader(),
rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;
oFReader.onload = function (oFREvent) {
if (!oPreviewImg) {
var newPreview = document.getElementById("imagePreview");
oPreviewImg = new Image();
oPreviewImg.style.width = (newPreview.offsetWidth).toString() + "px";
oPreviewImg.style.height = (newPreview.offsetHeight).toString() + "px";
newPreview.appendChild(oPreviewImg);
}
oPreviewImg.src = oFREvent.target.result;
};
return function () {
var aFiles = document.getElementById("imageInput").files;
if (aFiles.length === 0) { return; }
if (!rFilter.test(aFiles[0].type)) { alert("You must select a valid image file!"); return; }
oFReader.readAsDataURL(aFiles[0]);
}
}
if (navigator.appName === "Microsoft Internet Explorer") {
return function () {
alert(document.getElementById("imageInput").value);
document.getElementById("imagePreview").filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("imageInput").value;
}
}
})();
</script>
<style type="text/css">
#imagePreview {
width: 160px;
height: 120px;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
}
</style>
</head>
<body>
<div id="imagePreview">
</div>
<form name="uploadForm">
<p>
<input id="imageInput" type="file" name="myPhoto" onchange="loadImageFile();" /><br />
<input type="submit" value="Send" /></p>
</form>
</body>
</html>
但是需要注意的是 在IE8中 由于IE8自作聰明的將真實(shí)路徑隱藏起來了 用alert打印的結(jié)果是C:/fakepath/*.jpg 所以導(dǎo)致該方法無法使用
解決辦法是:進(jìn)入工具 -> Internet選項(xiàng) -> 安全 -> 自定義級別 -> 找到“其他”中的“將本地文件上載至服務(wù)器時(shí)包含本地目錄路徑”,選中“啟用”即可。
相關(guān)文章
CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
這篇文章主要介紹了CKEditor 4.4.1 添加代碼高亮顯示插件功能,涉及ckeditor使用官方推薦Code Snippet插件的相關(guān)操作布局與使用注意事項(xiàng),需要的朋友可以參考下2019-06-06前端JS,刪除JSON數(shù)據(jù)(JSON數(shù)組)中的指定元素方式
這篇文章主要介紹了前端JS,刪除JSON數(shù)據(jù)(JSON數(shù)組)中的指定元素方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js
腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js...2006-12-12JS實(shí)現(xiàn)用戶注冊時(shí)獲取短信驗(yàn)證碼和倒計(jì)時(shí)功能
在用戶注冊時(shí),通常需要短信驗(yàn)證碼,而且為了交互效果,也需要增加倒計(jì)時(shí)。該如何實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)功能呢?下面小編給大家分享JS實(shí)現(xiàn)用戶注冊時(shí)獲取短信驗(yàn)證碼和倒計(jì)時(shí)的代碼,一起看看吧2016-10-10BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法
這篇文章主要介紹了BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法,下面通過本文給大家詳細(xì)說明一下,需要的朋友可以參考下2016-12-12原生js實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制內(nèi)容到剪切板
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制內(nèi)容到剪切板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11JS實(shí)現(xiàn)簡單的todoList(記事本)效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)簡單的todoList(記事本)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08由JavaScript技術(shù)實(shí)現(xiàn)的web小游戲(不含網(wǎng)游)
伴隨Ajax與網(wǎng)頁游戲的崛起,曾幾何時(shí)JavaScript也成了游戲開發(fā)時(shí)可供選擇的技術(shù)之一,文本 僅列舉數(shù)項(xiàng)由JavaScript技術(shù)實(shí)現(xiàn)的web小游戲(不含網(wǎng)游),聊作參考之用。2010-06-06