• 二十七、单张图片上传预览


    html部分

    <div class="new-store-phone">
        <span class="phone-title">营业执照&nbsp;</span>
        <div class="phone-upload">
             <input type="file" name="file" id="file" class="inputfile"  onchange="changepic(this)" type="file" accept="image/*" />

            <img src="" id="show" style=" 100px; height: 100px;">

            <label for="file" class='btn btn-success' onchange="changepic(this)" type="file" accept="image/*">上传照片</label>
       </div>
    </div>

    js部分

    (1)第一部分是将图片路径渲染及上传

    function changepic(obj) {
    //console.log(obj.files[0]);//这里可以获取上传文件的name
    var newsrc = getObjectURL1(obj.files[0]);
    document.getElementById('show').src = newsrc;
    var imgType="storeLogo";
    var data={
    imgType:imgType ,
    }
    $.ajaxFileUpload({
    url : '/tsm/upload/filesUpload',
    secureuri : false, //一般设置为false
    fileElementId : 'file', //文件上传空间的id属性 <input type="file" id="file" name="file" />
    type : 'post',
    dataType : 'json', //返回值类型 一般设置为json
    data:data,
    success : function(data) { //服务器成功响应处理函数
    var result=data.result;
    if(result==0){
    var logoName=data.data[0];
    $("#imgUrl1").val(logoName);
    }
    },

    });

    }

    //建立一個可存取到該file的url
    function getObjectURL(file) {
    var url = null;
    // 下面函数执行的效果是一样的,只是需要针对不同的浏览器执行不同的 js 函数而已
    if (window.createObjectURL != undefined) { // basic
    url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
    url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
    url = window.webkitURL.createObjectURL(file);
    }
    return url;
    }

  • 相关阅读:
    JS实现添加至购物车功能
    python定制数据类型(继承与授权)两种方式
    模拟数据,控制账号
    绑定知识拓展
    面向对象之多态练习
    面向对象之继承与派生(学生管理系统)
    面向对象之组合(学生管理系统)
    一次小的上机试题
    面向对象之self classmethod staticmethod
    haproxy.conf文件操作(基于函数方式)
  • 原文地址:https://www.cnblogs.com/deng-jie/p/9370666.html
Copyright © 2020-2023  润新知