JS上传文件、导入文件

//开始导入
        function Import() {
            var filepath = $('#txtUpload').val();
            //校验是否选择表格
            if (filepath == '') {
                $('#showMsg').html('请选择表格');
                return;
            }

            var files = document.getElementById("txtUpload").files;
            var fd = new FormData();
            for (var i = 0; i < files.length; i++) {
                fd.append("file[" + i + "]", files[i]);
            }

            if (files[0].size > 4194304) {
                $('#showMsg').html('上传的大小不能大于4M');
                return;
            }
            $('#showMsg').html('<label style="color:coral;">正在上传表格,请稍等</label>');
            $.ajax({
                url: '@Url.Content("~/CheckConfiguration/upload")',
                type: "post",
                data: fd,
                dataType: "json",
                cache: false,//上传文件无需缓存
                processData: false,//用于对data参数进行序列化处理 这里必须false
                contentType: false,
                success: function (data) {
                    if (data.Code == 1) {                        
                        //setTimeout(ImportData, 2000);//测试用

                        ImportExcel(data.Msg); //data.Msg,代表完整路径
                    } else {
                        $('#showMsg').html('上传发生错误');
                    }
                },
                error: function () {
                    $('#showMsg').html('上传发生错误,请检查(表格名称不能包含特殊字符及大小不能超过4M)!');
                }
            });
        }
function ImportExcel(path) {
            $('#showMsg').html('<label style="color:coral;">上传成功,开始导入,请稍等</label>');
            $.ajax({
                url: '@Url.Content("~/CheckConfiguration/ImportExcel")',
                type: "post",
                data: { excelPath: path},
                dataType: "json",                 
                success: function (data) {
                    if (true) {
                        $('#showMsg').html('<label style="color:green;">导入成功,共导入1000条数据</label>');
                    } else if (false) {
                        $('#showMsg').html('导入失败:模板校验不对,请检查表格的头部格式');
                    } else if (false) {
                        $('#showMsg').html('导入失败:' + '<a href="/Template/Excel/HisEquipmentTemplate.xlsx">点击下载失败原因</a>');
                    } else if (true) {
                        $('#showMsg').html('导入失败:未将对象引入对象实例');
                    }
                },
                error: function () {
                    $('#showMsg').html('导入发生错误');
                }
            });
        }



 

 

天生我材必有用,千金散尽还复来
原文地址:https://www.cnblogs.com/ligenyun/p/9582913.html