ng-file upload支持分号分隔的csv文件,使用laravel 5.2&角度?

时间:2016-08-22 10:59:41

标签: angularjs laravel-5.2 ng-file-upload

我正在使用ng-file-upload上传文件,并使用文件中的加载数据将该文件导入到表中。它完美地用于逗号分隔,但我想以分号分隔导入文件。

我的前端是棱角分明的,后端是laravel 5.2

这是角度js

               $scope.uploadPic = function(file){
                console.log(Upload);
                file.upload = Upload.upload({
                    url: API_BASE_URL + "panelist/import-panelist",
                    data: {
                        file: file,
                        username: $scope.username, 
                        upload_type:$scope.panelistimport.upload_type,
                        panelist_status:$scope.panelistimport.panelist_status,
                        panel_id:1
                    },
                });

                file.upload.then(function (response) {
                    $timeout(function () {
                        file.result = response.data;
                        console.log( file.result);
                    });
                }, function (response) {
                    if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    // Math.min is to fix IE which reports 200% sometimes
                    file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                });
            }

在laravel控制器中 -

  public function importPanelistData(Request $request){


                        if($request->file('file') && $request->input('panel_id')){
                            $file = $request->file('file');
                            $data1 = Array();
                            $data1 = array('panel_id' => $request->input('panel_id'),
                                    'upload_type' => $request->input('upload_type'),
                                    'panelist_status' => $request->input('panelist_status')
                                );
                            $originalName = $file->getClientOriginalName();
                            $name = time() . '-' . $originalName;
                            $storage = 'upload'; 
                            $path = $storage . '/csv';
                            // Moves file to folder on server
                            $file->move($path, $name);
                            $importPanelist = $this->importPanelist($path, $name ,$data1);
                            return Response::json($importPanelist ? $this->makeResponse("Panelist Imported successfully.",$originalName) : 'No rows affected');
                        }

                    }


public function importPanelist($path, $name ,$extraFields = array()){

                    $csv = $path . "/" . $name; 
                    $query = "LOAD DATA LOCAL INFILE '$csv' INTO TABLE panelist FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8,@col9,@col10,@col11,@col12,@col13,@col14) set code=@col1,email=@col2,gender=@col3,postalcode=@col4,year_of_birth=@col5,email_secondary=@col6,first_name=@col7,middle_name=@col8,last_name=@col9,phone=@col10,phone_secondary=@col11,fax=@col12,fax_secondary=@col13,address=@col14,panel_id='".$extraFields['panel_id']."' ,status='".$extraFields['panelist_status']."' ";
                    return DB::connection()->getpdo()->exec($query);
                }

如果以分号分隔的作品,请帮助我。

1 个答案:

答案 0 :(得分:0)

我得到的解决方案终止于&#39 ;;'在下面的行。

 $query = "LOAD DATA LOCAL INFILE '$csv' INTO TABLE panelist FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8,@col9,@col10,@col11,@col12,@col13,@col14) set code=@col1,email=@col2,gender=@col3,postalcode=@col4,year_of_birth=@col5,email_secondary=@col6,first_name=@col7,middle_name=@col8,last_name=@col9,phone=@col10,phone_secondary=@col11,fax=@col12,fax_secondary=@col13,address=@col14,panel_id='".$extraFields['panel_id']."' ,status='".$extraFields['panelist_status']."' ";