zoukankan      html  css  js  c++  java
  • js图片上传及显示

    html部分:

    <form action='' method='post' name='myform'>
    <input type='file' id='iptfileupload' onchange='show()' value='' />
    <img src='1.jpg' alt='' id='img' />
    </form>

    js部分:

    <script type="text/javascript">
    function getPath(obj,fileQuery,transImg) {

    var imgSrc = '', imgArr = [], strSrc = '' ;

    if(window.navigator.userAgent.indexOf("MSIE")>=1){ // IE浏览器判断
    if(obj.select){
    obj.select();
    var path=document.selection.createRange().text;
    alert(path) ;
    obj.removeAttribute("src");
    imgSrc = fileQuery.value ;
    imgArr = imgSrc.split('.') ;
    strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
    if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
    obj.setAttribute("src",transImg);
    obj.style.filter=
    "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path+"', sizingMethod='scale');"; // IE通过滤镜的方式实现图片显示
    }else{
    //try{
    throw new Error('File type Error! please image file upload..');
    //}catch(e){
    // alert('name: ' + e.name + 'message: ' + e.message) ;
    //}
    }
    }else{
    // alert(fileQuery.value) ;
    imgSrc = fileQuery.value ;
    imgArr = imgSrc.split('.') ;
    strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
    if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
    obj.src = fileQuery.value ;
    }else{
    //try{
    throw new Error('File type Error! please image file upload..') ;
    //}catch(e){
    // alert('name: ' + e.name + 'message: ' + e.message) ;
    //}
    }

    }

    } else{
    var file =fileQuery.files[0];
    var reader = new FileReader();
    reader.onload = function(e){

    imgSrc = fileQuery.value ;
    imgArr = imgSrc.split('.') ;
    strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
    if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
    obj.setAttribute("src", e.target.result) ;
    }else{
    //try{
    throw new Error('File type Error! please image file upload..') ;
    //}catch(e){
    // alert('name: ' + e.name + 'message: ' + e.message) ;
    //}
    }

    // alert(e.target.result);
    }
    reader.readAsDataURL(file);
    }
    }

    function show(){
    //以下即为完整客户端路径
    var file_img=document.getElementById("img"),
    iptfileupload = document.getElementById('iptfileupload') ;
    getPath(file_img,iptfileupload,file_img) ;
    }

    </script>

    效果:

  • 相关阅读:
    注意技巧小结
    小数点转百分比
    Mysql 性能监控及调优
    Linux下快速安装Mysql及使用
    如何建立压力计算模型
    Redis 2种持久化模式的缺陷
    Redis性能调优建议
    Node.js学习入门手册
    手机APP脚本录制(LoadRunner 12)
    Linux系统内核性能调优
  • 原文地址:https://www.cnblogs.com/lvlina/p/10160610.html
Copyright © 2011-2022 走看看