javascript - 如何在angularjs中获取输入类型文件的值?

标签 javascript php angularjs slim

我正在使用 slim 框架在 angularjs 中创建简单的应用程序。在其中,我正在为一些数据(如名称、类别、货币和图像文件等)创建表单。在这个应用程序中,我在输入类型文件方面遇到问题。我可以获取文本框或单选按钮等的所有数据,但无法获取输入类型文件的数据。下面是代码。

index.html:- HTML 文件

<form method="POST" enctype="multipart/form-data" class="form account-form" name="form" ng-submit="creator(user)" role="form">
<input type="text" name="name" ng-model="user.name"  /> 

<input type="radio" name="category" ng-model="user.category" value="Science" >                                  
<input type="radio" name="category" ng-model="user.category" value="Engineerig" >

<input type="file" ng-model="user.image_file" name="image_file">
</form>

registerController.js:- Controller 文件

app.controller('CreatorController', function($scope,$rootScope,$location,$http,CreatorService,FlashService) {
    $scope.creator = function (user) {
      // alert("create");
        $scope.dataLoading = true;
         CreatorService.creator(user)
                .then(function (response) {   
                    if(response.data['message']){ 
                        FlashService.Success('Registration successful', true);
                        $location.path('/crete-page');
                    } else {
                        FlashService.Error('Registration failed');
                    }
                });
    }    
});

registerDB.php :- slim 框架目录中的 DB 处理程序 PHP 文件

 <?php

function insertUser($data) {

    print_r($data); //Getting All data here from form but not getting File values
}
?>

最佳答案

如果您想将其发送到服务器,您不能只接收文件并发送它,有时您必须将其转换为 blob 或其他内容。

我找到了这个fiddle

don't know if it works but you can try. (stupid stackoverflow makes me put code after a fiddle link, wtf!)

或者您可以使用angular-file-upload ,至少是我尝试做类似事情时使用的。但我在服务器上使用nodejs,甚至在服务器上我也必须使用另一个特定于这种情况的库。不知道它如何与 php 一起工作。

GL!

关于javascript - 如何在angularjs中获取输入类型文件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523505/

相关文章:

javascript - 无法读取未定义的属性 'thisCompilation' - react-scripts-ts

javascript - 如何使用目标 div 中的数据属性作为 PrimeFaces ContextMenu 结果中的 View 参数

php - 直接 MySQL 结果与转换为 JSON?

javascript - 如何使对象/属性显示在指令中?

javascript - 很好地使用 requirejs 或 grunt 与 concat/minification 来获得 angularjs 性能

php - 为什么使用 "AND"运算符与 JOIN 一起工作,即使没有 "WHERE"子句

php:是否有充分的理由引用所有数组键/索引?

javascript - 选择元素AngularJS的默认值

javascript - ng-repeat 仅显示对象数组中的第一项

javascript - 仅当没有输入被聚焦时才将第一个输入聚焦于加载