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('導入發生錯誤');
                }
            });
        }
 
         

 

 
        

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM