ds355020 2015-12-19 09:49
浏览 57

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

I am trying to upload multiple files with different title but unable to get uploaded file names.

Here is my html code.

 <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>

Here is the angularjs Script.

 //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;
                    });
                }
            };      
});

Here is php code for file upload

<?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);

?>

Here is the final code to insert details into database.

$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;
}

Getting Result as below {"idno":"20","pname":"Vijay","TestName":"OCT","FileName":"","Message":""}{"idno":"25","pname":"Salim","TestName":"FFA","FileName":"","Message":""}

Getting above result data except uploaded file. Please help me to solve this issue.

Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。