通過ajax上傳表單文件


html部分:

<form method="post" id="productUploadForm" name="frmBatchSettle" enctype="multipart/form-data" encoding="multipart/form-data">
    <input type="file" class="col-xs-4 up_file" name="productFile" value="" id="filename">
    <a class="btn btn-xs btn-primary btn-outline col-xs-2 product_upload"><span class="fa fa-upload"></span> 上傳商品</a>
</form>

js部分:

  //通過上傳表格,添加商品數據
        $(".product_upload").live("click",function () {
            var formData = new FormData($("#productUploadForm")[0]);
            $.ajax({
//                async : flase,
//                cache : flase,
                type : "post",
                data : formData,
                url : '/product/product_data_upload',   //ajax上傳的路徑
                dataType : 'json',
                contentType: false, //必須,告訴jquery不要設置Content-Type請求頭
                processData: false, //必須,告訴jquery不要處理發送的數據
                success : function(res) {
                    if(res.error==0)
                    {
                        //數據正常,添加商品
                        product  = res.content.reason;
                        for(var i = 0;i<product.length;i++)
                        {
                var html = '<tr class="gradeC one-product" >'
                                +  '<td class="product-id">'+product[i]['f_id']+'</td>'
                                +  '<td class="product-name">'+product[i]['f_name']+'</td>'
                                +  '<td class="product-self-id">'+product[i]['f_self_id']+'</td>'
                                +  '<td class="product-bar-code">'+product[i]['f_bar_code']+'</td>'
                                +  '<td class="product-warn-number">'+ product[i]['f_warn_number']+'</td>'
                                +  '</tr>';
                            $("table:first tbody").append(html);
                        }
                    }
                    else if(res.error==1) {
                        alert(res.reason);
                    }
                    else {
                        //上傳數據出錯,頁面提示
                        alert('表格數據錯誤,請修改后重新上傳');
                    }
                },
                error : function(res) {
                }
            });
        });

控制器Controller:

  //上傳商品的數據
    public function actionProduct_data_upload()
    {
        header('Content-Type:text/html;charset=utf-8 ');
        //php處理文件的時間。 為0表示沒有時間限制
        set_time_limit(0);
        require_once(FRAMEWORK_ROOT . '/extensions/excel/PHPExcel/Reader/Excel5.php');
        require_once(FRAMEWORK_ROOT . '/extensions/excel/PHPExcel/Reader/Excel2007.php');

        if (isset($_FILES['productFile']) && $_FILES['productFile']['error'] == 0)
        {
            /**默認用excel2007讀取excel,若格式不對,則用之前的版本進行讀取*/
            $filePath = $_FILES['productFile']['tmp_name'];
            $PHPReader = new PHPExcel_Reader_Excel2007();

            //判斷是否是xsl文件
            if (!$PHPReader->canRead($filePath))
            {
                $PHPReader = new PHPExcel_Reader_Excel5();
                if (!$PHPReader->canRead($filePath))
                {
             EApi::Error(2,"文件格式錯誤!");
} } $PHPExcel = $PHPReader->load($filePath); /**讀取excel文件中的第一個工作表*/ $currentSheet = $PHPExcel->getSheet(0); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); //行數 $remind = array(); $product = array(); //商品數據無誤的情況下,上傳信息 $i = 0; //商品信息錯誤的條數 $j =0; //商品信息無誤的條數 for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $product_name = trim(trim($currentSheet->getCell('A' . $currentRow)->getValue()), " "); $product_code = trim(trim($currentSheet->getCell('B' . $currentRow)->getValue()), " "); $product_price = trim(trim($currentSheet->getCell('C' . $currentRow)->getValue()), " "); $product_num = trim(trim($currentSheet->getCell('D' . $currentRow)->getValue()), " "); $flag = 0; //判斷該行數據有沒有出錯 //驗證上傳的數據是否符合要求。     if (empty($product_code)) { $remind["$i"] .= "第".$currentRow."行"."商品條形碼不能為空,"; $flag = 1; } else { //查詢該條形碼是否存在 $exists = T_product::model()->exists("f_bar_code='{$product_code}' AND f_is_shelves=1"); if (!$exists) { $remind["$i"] .= "第".$currentRow."行"."商品條形碼不存在或未上架,"; $flag = 1; } }           if($flag==0) { $detail = T_product::model()->find("f_bar_code='{$product_code}' AND f_is_shelves=1"); if($detail->f_id) { $product["$j"]['f_id'] = $detail->f_id; $product["$j"]['f_name'] = $detail->f_name; $product["$j"]['f_self_id'] = $product_code; $product["$j"]['f_bar_code'] = $product_code; $product["$j"]['f_warn_number'] = $detail->f_warn_number; $product["$j"]['qty'] = $product_num; $product["$j"]['price'] = $product_price; $j++; } } $i++; } //上傳數據格式不對。將錯誤信息返回到頁面 if (count($remind)>=1) { $out = array( 'error' => 2, 'reason' => $remind, ); EApi::Error($out); } else { $out = array( 'error' => 0, 'reason' => $product, ); EApi::Success($out); } } else { EApi::Error(1, "請先選擇上傳的文件!"); } }

 

 
       


免責聲明!

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



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