zoukankan      html  css  js  c++  java
  • 通过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, "请先选择上传的文件!"); } }
    
    
  • 相关阅读:
    kickstart_Round C 2020
    4.26腾讯笔试题
    [jvm][面试]JVM 调优总结
    Django中的Model(操作表)
    C++ 无锁数据结构
    masstree Seastar
    java logAspect
    vimrc
    GopherChina 2018
    RocketMQ
  • 原文地址:https://www.cnblogs.com/zwtqf/p/9383383.html
Copyright © 2011-2022 走看看