栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

form+iframes模拟文件上传

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

form+iframes模拟文件上传

1 DOM结构

2 JS部分

//上传文件
//监听frame的 onload方法
let oFrm = document.getElementById("uploadframe");
oFrm.onload = oFrm.onreadystatechange = function () {
    if (this.readyState && this.readyState != "complete") return;
    //获取iframe里面的内容
    let responseText = document.getElementById("uploadframe").contentWindow.document.getElementsByTagName("BODY")[0].innerText;
    //上传完成后的处理
    if (responseText != "") {
 let responseData = JSON.parse(responseText);
 // alert(JSON.parse(responseText));
 $('.update-file .selected-option').show();
 $('.update-file .selected-option ul').html('
  • + responseData.RetValue.filePath + '">' + responseData.RetValue.fileName + '
  • '); } } //监听文件路径变化 $("#fileLocalFile").change(function () { let filePath = $("#fileLocalFile").val(); if (filePath != "") { let extionType = filePath.substr(filePath.lastIndexOf(".") + 1); if (extionType != "xls" && extionType != 'xlsx' && extionType != 'jpg' && extionType != 'jpeg' && extionType != 'pdf') { alert("请上传jpg、pdf、excle等格式,大小1.5M以内的文件"); $("input[name=fileLocalFile]").val(""); return; } else { if ($('.update-file .selected-option ul').html() == '') { let index = filePath.lastIndexOf("\"), fileName = filePath.substring(index + 1, filePath.length); $('#upload')[0].submit(); $("input[name=fileLocalFile]").val(""); } else { //确认弹层 $('.yi-mask').show(); $('#/confirm/iD').show(); } } } }); $('#confirmD .btnok').click(function () { $('#/confirm/iD').hide(); $('.yi-mask').hide(); let filePath = $("#fileLocalFile").val(); let index = filePath.lastIndexOf("\"), fileName = filePath.substring(index + 1, filePath.length); $('#upload')[0].submit(); $("input[name=fileLocalFile]").val(""); }); //弹层关闭 $('#confirmD .close,#confirmD .btnok').click(function () { $('.yi-mask').hide(); $(this).parents('.yi-modal').hide(); $("input[name=fileLocalFile]").val(""); })

    3 CSS部分

    #upload {
        position: relative;
        float: left;
    }
    
    #fileLocalFile {
        opacity: 0;
        filter: alpha(opacity=0);
        position: absolute;
        left: 0;
        top: 0;
        width: 70px;
        height: 30px;
        z-index: 1;
        cursor: pointer;
    }
    

    总结:

    a>
    不要用其他事件来绑定file的点击事件,会造成ie8拒绝访问;
    如:

     $("#uploadBtn").click(function() {
      	$("#fileLocalFile").trigger("click");
     });
    

    b>
    接口要返回字符串类型的数据,ie8接收不到json类型

    转载请注明:文章转载自 www.mshxw.com
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号