无法使用带有php的angularjs获取上传的文件名

时间:2015-12-19 09:49:24

标签: php angularjs multiple-file-upload addremoveprograms

我正在尝试上传多个具有不同标题但无法上传文件名的文件。

这是我的HTML代码。

 <div class="nav-md" ng-app="fileuploadModule">
        <div class="container body" ng-controller="fileuploadController as parentCtrl">
            <div class="form-group">
                <div class="col-sm-6" style="padding:0px 10px 0px 0px;">
                    <label class="control-label">ID-NO</label>
                    <input type="text" class="form-control" id="idno" ng-model="idno" />    

                </div>
            <div class="col-sm-6" style="padding:0px 10px 0px 0px;">
                <label class="control-label" >Name</label>
                <input type="text" class="form-control" id="pname" ng-model="pname" />  
            </div>
        </div>
        <div class="form-group" ng-controller="ChildController as childCtrl">
            <div class="col-md-12" style="padding:0px 10px 0px 0px;">
                <table ng-table="tableParams" class="display table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th width="5%">Sr.No</th>
                            <th width="35%">Title</th>
                            <th width="35%">Upload Report</th>
                            <th width="25%"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="cap in data">
                            <td>{{$index+1}}</td>
                            <td>
                                <input class="form-control" id="tags" type="text" ng-model="cap.name" placeholder="Title" typeahead="drug.name for drug in drugArray | filter:$viewValue | limitTo:4">                   
                            </td>
                            <td>
                                <input type="file" id="i_file" name="file" ng-model="cap.file" ng-file-select="onFileSelect($files)" />
                                <input type="hidden" ng-model="cap.message" value={{message}} />
                            </td>
                            <td>
                                <input type="submit" ng-click="addFormField()" class="btn btn-default" value="Add Items" /> 
                                <input type="submit" value="Remove" class="btn btn-primary" ng-click="removeRow(cap.name)" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

这是angularjs脚本。

 //reports upload
    var infu = angular.module('fileuploadModule',['angularFileUpload','angularUtils.directives.dirPagination','ui.bootstrap'])
    infu.controller('fileuploadController', function ($scope, $http) 
{

/*Add & Remove Script Starts Here*/
    //to remove the row
    $scope.removeRow = function(name) {
        var index = -1;
        var comArr = eval( $scope.data );
        for( var i = 0; i < comArr.length; i++ ) {
            if( comArr[i].name ==name ) {
                index = i;
                break;
            }
        }
        if( index === -1 ) {
        }
        $scope.data.splice( index, 1 );
    };

    //dynamic row creation
    $scope.data = [{}];

    //Autocomplete for title
    $scope.drugArray = [];
    $http({method: 'GET',url: 'get_test_name.php',data: { applicationId: 3 }}).success(function (data) {
        $scope.drugArray = data;
    });

    $scope.addFormField = function() {
        $scope.data.push({});
    }
/*Add & Remove Script Ends Here*/


    //Updated Investigation
    $scope.submitData = function()
    {
        var comArr = eval( $scope.data );
        var name=new Array();
        var file=new Array();
        var message=new Array();
        for( var i = 0; i < comArr.length; i++ )
        {
            name.push(comArr[i].name);
            file.push(comArr[i].file);
            message.push(comArr[i].message);
        }

        var answer = confirm("Do you want to Submit?")
        if (!answer)
        {

        }   
        else
        {
            $http.post('update_details.php', {
                'idno': $scope.idno,
                'pname': $scope.pname,
                'name': name,
                'file': file,
                'message': message,
                /*'filename': $scope.parentCtrl.childCtrl.message //If i give like getting only last upload file name*/
            })
            .success(function (data, status, headers, config)
            {           
                alert("Data has been Added Successfully");
                window.location.href="list_investigation_reports.php";
            })

            .error(function(data, status, headers, config)
            {

            });         
        }
    }   
});

//ChildController For file Upload
infu.controller('ChildController',function ($scope, $http, $upload) {        
 $scope.parentCtrl.childCtrl = $scope.childCtrl;        
    $scope.onFileSelect = function($files) {
                $scope.message = "";
                for (var i = 0; i < $files.length; i++) {
                    var file = $files[i];
                    console.log(file);
                    $scope.upload = $upload.upload({
                        url: 'upload.php',
                        method: 'POST',
                        file: file
                    }).success(function(data, status, headers, config) {

                        $scope.message = data;
                    $scope.message=$scope.message.replace('\"', '');
                        $scope.childCtrl.message=$scope.message.replace('\"', '');                          
                    }).error(function(data, status) {
                        $scope.message = data;
                    });
                }
            };      
});

这是用于文件上传的php代码

<?php
$target_dir = "uploadfiles/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);

$result = array("filename" => $_FILES["file"]);


$filename = $result['filename']['name'];
echo json_encode($filename);

?>

以下是将详细信息插入数据库的最终代码。

$data = json_decode(file_get_contents("php://input"));
$idno = $data->idno; 
$pname = $data->pname;

$tname = $data->name;
$filename = $data->file;
$message = $data->message;

$date=date('Y-m-d');

foreach($tname as $index => $value)
{
    $name1 = $tname[$index];
    $filename1 = $filename[$index];
    $message1 = $message[$index];   

    $qry_ress = $dbConnection->prepare("INSERT INTO tablename(idno,pname,testname,filename,cts) VALUES (?,?,?,?,?)");   
    $qry_ress->execute(array($idno,$pname,$name1,$filename1,$cts));

    $arr = array("idno" => "$idno","pname" => "$pname","TestName" => "$name1","FileName" => "$filename1");
    $jsn = json_encode($arr);
    echo $jsn;
}

获得如下结果 { “IDNO”: “20”, “pname中”: “维杰”, “测试名”: “OCT”, “文件名”: “”, “消息”: “”} { “IDNO”: “25”,“pname中“:” 姆”, “测试名”: “FFA”, “文件名”: “”, “消息”: “”}

获取除上传文件之外的结果数据。请帮我解决这个问题。

提前致谢。

0 个答案:

没有答案