zoukankan      html  css  js  c++  java
  • Excel文件上传功能实现

    $(function(){
    let file;
    //上传点击事件
    $('#btn').bind('click', function(){
    let val = $('#fb').filebox('getText');
    if(!val){
    return alert('未选择任何文件!')
    }
    let reg = /.(xlsx|xls)/i;
    if(reg.test(val)){
    let formData = new FormData();
    formData.append("upfile", file); //upfile 后台接收的参数名
    $.ajax({
    url: '', //url地址,向后台请求的,该是什么写什么
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    beforeSend: function(xhr) {
    if (accesstoken && username && userid) {//用户信息,一般记录是session里面
    xhr.setRequestHeader("AccessToken",
    accesstoken);
    xhr.setRequestHeader("username", username);
    xhr.setRequestHeader("userid", userid);
    }
    },
    success: function (res) {
    if(!res || res.hasError && res.error.indexOf('请重新登录') >= 0){
    $UToolNoCache();//清楚缓存的封装方法,需要的自己写逻辑
    return;
    }
    if(res && res.hasError){
    return alert(`上传失败:<br/> ${res.error}`);
    }else{
    return alert('上传成功!');
    }
    },
    error: function (res) {
    console.log(res);
    return alert('请求发送失败!');
    }
    });

    }else{
    $('#fb').filebox('setText','');
    return alert('请选择正确的Excel文件!');
    }
    });
    //文件判断
    $(':file').change(function(){
    file = this.files[0];
    // let name = file.name;
    // let size = file.size;
    let type = file && file.type;
    if(type !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' && type !== 'application/vnd.ms-excel'){
                       return alert('此处应选择Excel文件!');
            }
    });
    });
  • 相关阅读:
    UVALive 6909 Kevin's Problem 数学排列组合
    UVALive 6908 Electric Bike dp
    UVALive 6907 Body Building tarjan
    UVALive 6906 Cluster Analysis 并查集
    八月微博
    hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形
    hdu 5792 World is Exploding 树状数组
    hdu 5791 Two dp
    hdu 5787 K-wolf Number 数位dp
    hdu 5783 Divide the Sequence 贪心
  • 原文地址:https://www.cnblogs.com/geqin/p/9437342.html
Copyright © 2011-2022 走看看