ng-file upload not sending file along with other data - angularjs

i am using angular js frontend along with ng-file module and laravel backend, and for some reason unknown, ng fileupload doesn't send file along with other form data:
here is my form:
<form method="post" name="myForm">
<div class="form-group has-feedback">
<input type="file" ngf-select ng-model="picFile" name="file"
accept="image/*" ngf-max-size="2MB" required
ngf-model-invalid="errorFile">
<i ng-show="myForm.file.$error.required">*required</i><br>
<i ng-show="myForm.file.$error.maxSize">File too large
{{errorFile.size / 1000000|number:1}}MB: max 2M</i>
<img ng-show="myForm.file.$valid" ngf-thumbnail="picFile" class="thumb"> <button ng-click="picFile = null" ng-show="picFile">Remove</button>
</div>
<div class="form-group has-feedback">
<input class="form-control input-lg" id="username" type="text" name="username" ng-model="user.username" placeholder="Name"
required
ng-minlength="6"
autofocus>
</div>
<div class="form-group has-feedback">
<input class="form-control input-lg" type="email" id="email" name="email" ng-model="user.email" placeholder="Email" required>
</div>
<div class="form-group has-feedback">
<input class="form-control input-lg" type="text" id="firstname" name="firstname" ng-model="user.firstname" placeholder="First Name">
</div>
<div class="form-group has-feedback">
<input class="form-control input-lg" type="text" id="lastname" name="lastname" ng-model="user.lastname" placeholder="Last Name">
</div>
<button type="submit" class="btn btn-lg btn-block btn-primary" ng-disabled="!myForm.$valid" ng-click="updateProfile(picFile)" >Update</button>
<span class="progress" ng-show="picFile.progress >= 0">
<div style="width:{{picFile.progress}}%"
ng-bind="picFile.progress + '%'"></div>
</span>
<span ng-show="picFile.result">Upload Successful</span>
<span class="err" ng-show="errorMsg">{{errorMsg}}</span>
{{picFile}}
</form>
and here is my controller:
$scope.getProfile = function() {
Account.getProfile()
.then(function(response) {
$scope.user = response.data;
})
.catch(function(response) {
toastr.error(response.data.message, response.status);
});
};
$scope.updateProfile = function(file) {
file.upload = Upload.upload({
url: '/kilo/api/me',
data: {username: $scope.user.username, file: file },
method:"POST"
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data.message;
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));
});
};
the above sends only the "username" and leaves the file. please can anyone show me what am doing wrong?

Add arrayKey: '', headers: { 'Content-Type': undefined }, to your upload object.

Related

AngularJS and ASP.NET MVC data insert issue

I am inserting Data using AngularJS and ASP.NET MVC.
But I am facing issue that the data is inserted null.
What should I do?
This is AngularJS file:
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, $http) {
debugger;
$scope.InsertData = function () {
var Action = document.getElementById("btnSave").getAttribute("value");
if (Action == "Submit") {
$scope.User = {};
$scope.User.Username = $scope.Username;
alert($scope.Username);
$scope.User.Password = $scope.Password;
$scope.User.Email = $scope.Email;
$scope.User.Phone = $scope.Phone;
$scope.User.DOB = $scope.DOB;
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: JSON.stringify($scope.User)
}).then(function (response) {
alert("inserted");
});
}
};
});
This is HTML file:
<div class="tab-content" ng-controller="myCtrl">
<div class="tab-pane active" id="register">
<h3>Register Now !!!</h3>
<p class="text-muted">Be cool and join today. Meet millions</p>
<!--Register Form-->
<form name="registration_form" id='registration_form' class="form-inline">
<div class="row">
<div class="form-group col-xs-6">
<label for="Username" class="sr-only">User Name</label>
<input id="Username" ng-model="Username" class="form-control input-group-lg" type="text" name="Username" title="Enter User name" placeholder="User name" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="email" class="sr-only">Email</label>
<input ng-model="Email" id="email" class="form-control input-group-lg" type="text" name="Email" title="Enter Email" placeholder="Your Email" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="Phone" class="sr-only">Phone</label>
<input ng-model="Phone" id="Phone" class="form-control input-group-lg" type="text" name="Phone" title="Enter Phone" placeholder="Your Phone" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="password" class="sr-only">Password</label>
<input ng-model="Password" id="password" class="form-control input-group-lg" type="password" name="password" title="Enter password" placeholder="Password" />
</div>
</div>
<div class="row">
<p class="birth"><strong>Date of Birth</strong></p>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="day">
<option value="Day" disabled selected>Day</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</div>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="month">
<option value="month" ng-model="DOB" disabled selected>Month</option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
</div>
<div class="form-group col-sm-6 col-xs-12">
<label for="year" class="sr-only"></label>
<select class="form-control" id="year">
<option value="year" disabled selected>Year</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>
</div>
</div>
<div class="form-group gender">
<label class="radio-inline">
<input type="radio" name="optradio" checked>Male
</label>
<label class="radio-inline">
<input type="radio" name="optradio">Female
</label>
</div>
</form><!--Register Now Form Ends-->
<p>Already have an account?</p>
<button id="btnSave" value="Submit" class="btn btn-primary" ng-click="InsertData()">Register Now</button>
</div>
</div>
This is ASP.NET controller:
public string Insert_Employee(User User)
{
if (User != null)
{
using (Database1Entities Obj = new Database1Entities())
{
Obj.Users.Add(User);
Obj.SaveChanges();
return "Employee Added Successfully";
}
}
else
{
return "Employee Not Inserted! Try Again";
}
}
I am entering data, but data is inserted as null into db. What should I do no?
I have added <body ng-app="myApp">
According to me there is no error
can anyone help?
Try to wrote in this way your ajax call:
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: {user:$scope.User}
}).then(function (response) {
alert("inserted");
});

Not able to pass value in angular function

I am trying to pass expense object to submit function.
But it is not passing entered value
var app = angular.module('addApp', []);
app.controller('addController', function($scope, $http) {
console.log("SAdfasd");
$scope.expense = {param:'',value:0,dt:'',description:''};
$scope.exp = ["One ", "Two", "Three"];
$scope.submit = function(data) {
console.log(data);
console.log($scope.expense);
$http({
method: 'POST',
url: 'added'
data: $scope.expense,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, header, config) {
console.log(data);
}
});
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"></script>
<div class="col-lg-8 col-md-8" ng-app="addApp" ng-controller="addController">
<form id="addExpense" name="expenseForm" method="post">
<div>
<label>Expense Type</label>
<select ng-model="expense.param" ng-options="x for x in exp" class="form-control"></select>
</div>
<div>
<label>Amount</label>
<input type="text" name="value" class="form-control" ng-bind="expense.value" placeholder="Enter your expense amount" />
</div>
<div>
<label>Date</label>
<input type="date" name="dt" class="form-control" ng-bind="expense.dt" placeholder="Select expense date" />
</div>
<div>
<label>Description</label>
<textarea rows="4" cols="50" name="description" ng-bind="expense.description" class="form-control" placeholder="Enter your expense description"></textarea>
</div>
<div>
<button type="submit" class="btn btn-success form-control" ng-click="submit(expense)">Save</button>
</form>
</div>
You are using NgBind instead of that it should be NgModel :
var app = angular.module('addApp', []);
app.controller('addController', function($scope, $http) {
$scope.expense=
{param:'',value:0,dt:'',description:''};
$scope.exp = ["One ", "Two", "Three"];
$scope.submit = function() {
//console.log(object);
console.log($scope.expense);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"></script>
<div class="col-lg-8 col-md-8" ng-app="addApp" ng-controller="addController">
<form id="addExpense" name="expenseForm" method="post">
<div>
<label>Expense Type</label>
<select ng-model="expense.param" ng-options="x for x in exp" class="form-control"></select>
</div>
<div>
<label>Amount</label>
<input type="text" name="value" class="form-control" ng-model="expense.value" placeholder="Enter your expense amount" />
</div>
<div>
<label>Date</label>
<input type="date" name="dt" class="form-control" ng-model="expense.dt" placeholder="Select expense date" />
</div>
<div>
<label>Description</label>
<textarea rows="4" cols="50" name="description" ng-model="expense.description" class="form-control" placeholder="Enter your expense description"></textarea>
</div>
<div>
<button type="submit" class="btn btn-success form-control" ng-click="submit(expense)">Save</button>
</form>
</div>
You are sending JSON obejct to your action but it is expecting it in a Url Params. You can refer your query from here ParamSerializer.
So angular provide $httpParamSerializerJQLike service since 1.4.1
$http({
url: 'added',
method: 'POST',
data: $httpParamSerializerJQLike(data), // Make sure to inject the
service in to the controller
headers: {
'Content-Type': 'application/x-www-form-urlencoded' // header
}
}).then(function(response) { /* do something here */ });
Second way to do -
$http({
url: 'added',
method: 'POST',
data: 'param='+$scope.expense.param+'&value='+$scope.expense.value+'& dt='+$scope.expense.dt+ '& description='+$scope.expense.description OR 'param='+encodeURIComponent($scope.expense.param)+'&value='+encodeURIComponent$scope.expense.value)+'& dt=+encodeURIComponent$scope.expense.dt)+ '& description='+encodeURIComponent$scope.expense.description)
service in to the controller
headers: {
'Content-Type': 'application/x-www-form-urlencoded' // header
}
}).then(function(response) { /* do something here */ });
Please check the syntax's for comma i might have missed .

Getting "Error: [$rootScope:infdig] 10 $digest() iterations reached" message

I want to display http response (JSON response) on html page in a table. To achieve this I wrote below controller function.
$scope.getSensorInfo=function(){
var strippeddata = [];
$http({
method: "GET",
url: "http://xx.xx.xx.xx:4000/config",
params: {did: $scope.cid}
}).then(function (success) {
if (success.data[0] === undefined) { //no device is available with this id
$http({
method: "GET",
url: "http://xx.xx.xx.xx:4000/info"
}).then(function(success1){
$scope.sinfo = success1.data;
},function(error1){
console.log('Error ' + JSON.stringify(error1));
});
} else {
Object.getOwnPropertyNames(success.data[0].obj).forEach(function (item, idx, list) {
Object.defineProperty(strippeddata, idx, {
enumerable: true,
configurable: true,
get: function () {
return {
name: item,
periodicity: success.data[0].obj[item].periodicity,
}
}
});
});
$scope.sinfo = strippeddata;
}
}, function (error) {
console.log('Error ' + JSON.stringify(error));
});
}
My html code is as follows.
<div class="row">
<div class="form-inline form-group">
<div class="col-md-3">
<label for="cnfdeviceid">Device ID</label>
<input class="form-control input-md" type="text" id="cnfdeviceid" placeholder="e.g. HYD01" ng-model="confdeviceid" ng-blur="getSensorInfo()"/>
</div>
</div>
</div>
<div ng-repeat="y in sinfo" class="[ form-group form-inline ]">
<input class="form-control input-md" type="checkbox" id="{{y.name}}" ng-model="name" ng-change="verify(y.name,y.periodicity,y.formula,name,$index)" autocomplete="off"/>
<div class="[ btn-group ]">
<label for="{{y.name}}" class="[ btn btn-success ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="{{y.name}}" class="[ btn btn-default active ]">
{{y.name}}
</label>
</div>
<input class="form-control input-md" type="text" ng-model="y.periodicity" placeholder="periodicity" id="{{y.periodicity}}" autocomplete="off"/>
</div>
When I am executing this code I am getting $digest 10 iterations reached aborting error. The following line is causing the problem but I am unable to rectify it.
$scope.sinfo = strippeddata;
Changing $scope.sinfo = strippeddata; to $scope.sinfo = strippeddata.slice(); resolved the issue.

Showing json data in textbox

I want to show json data to text box. but it is not showing in text box
my code is
var app = angular.module('productApp', []);
app.controller('productController', function($scope, $http) {
var x = 1054;
alert(x);
$http({
method: 'GET',
url: 'getProduct',
params: {
id: x
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(response, status, headers, config) {
console.log(response);
$scope.products = response;
})
.error(function(data, status, headers, config) {
alert("Opps unable to connect to server");
});
}); <
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<div class="container">
<div class="all-data" ng-app="productApp" ng-controller="productController">
<form id="updateProduct" action="UpdatedProduct" method="post">
<legend>Update Product</legend>
<div class="products" ng-repeat="p in products">
{{p.id}}{{p.unitPrice}}
<input type="hidden" ng-model="p.id" />
<label>Bar Code</label>
<input type="text" name="barCode" class="form-control" id="barCode" ng-model="p.barcode" />
<label> Quantity </label>
<input type="text" name="quantity" ng-model="p.quantity" class="form-control" id="quantity" />
<label>Unit Price</label>
<input type="text" name="unitPrice" ng-model="p.unitPrice" class="form-control" id="unitPrice" />
<label>Selling Price</label>
<input type="text" name="sellingPrice" class="form-control" id="sellingPrice" ng-model="p.sellingPrice" data-validation="required" data-validation-error-msg="Selling price required" />
<label>Discount Percentage</label>
<input type="text" name="discountPercentage" class="form-control" id="discountPercentage" ng-model="p.discountPercentage" />
<label>Tax1</label>
<input type="text" name="tax1name" class="form-control" id="tax1name" ng-model="p.tax1.name" />
<input type="hidden" name="tax1id" class="form-control" id="tax1id" ng-model="p.tax1.id" />
<input type="hidden" name="tax1value" class="form-control" id="tax1val" ng-model="p.tax1.value" />
<label>Tax2</label>
<input type="text" name="tax2name" class="form-control" id="tax2name" ng-model="p.tax2.name" />
<input type="hidden" name="tax2id" class="form-control" id="tax2id" ng-model="p.tax2.id" />
<input type="hidden" name="tax2value" class="form-control" id="tax2val" ng-model="p.tax2.value" />
<button type="submit" class="form-control btn btn-success" style="margin-top: 10px" ng-click="update(p)">Update</button>
</div>
</div>
my json data is
{"productList":[{"discountPercentage":3,"id":1054,"quantity":234,"sellingPrice":555.00,"tax1id":0,"tax1value":0,"tax2id":0,"tax2value":0,"unitPrice":234.00}]}
Try this one
}).success(function(response, status, headers, config) {
console.log(response);
$scope.products = response.productList;
})
Use response.data.productList,
$http({
method: 'GET',
url: 'getProduct',
params: {
id: x
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(response, status, headers, config) {
console.log(response);
$scope.products = response.data.productList;
})
.error(function(data, status, headers, config) {
alert("Opps unable to connect to server");
});
You either need to iterate over products.productList or, assign $scope.products = response.productList.
Here's working example: (I have used $timeout to simulate API call)
var app = angular.module('productApp', []);
app.controller('productController', function($scope, $timeout) {
var x = 1054;
alert(x);
$timeout(function() {
$scope.products = {
"productList": [{
"discountPercentage": 3,
"id": 1054,
"quantity": 234,
"sellingPrice": 555.00,
"tax1id": 0,
"tax1value": 0,
"tax2id": 0,
"tax2value": 0,
"unitPrice": 234.00
}]
}
}, 1000)
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<div class="container">
<div class="all-data" ng-app="productApp" ng-controller="productController">
<form id="updateProduct" action="UpdatedProduct" method="post">
<legend>Update Product</legend>
<div class="products" ng-repeat="p in products.productList">
{{p.id}}{{p.unitPrice}}
<input type="hidden" ng-model="p.id" />
<label>Bar Code</label>
<input type="text" name="barCode" class="form-control" id="barCode" ng-model="p.barcode" />
<label> Quantity </label>
<input type="text" name="quantity" ng-model="p.quantity" class="form-control" id="quantity" />
<label>Unit Price</label>
<input type="text" name="unitPrice" ng-model="p.unitPrice" class="form-control" id="unitPrice" />
<label>Selling Price</label>
<input type="text" name="sellingPrice" class="form-control" id="sellingPrice" ng-model="p.sellingPrice" data-validation="required" data-validation-error-msg="Selling price required" />
<label>Discount Percentage</label>
<input type="text" name="discountPercentage" class="form-control" id="discountPercentage" ng-model="p.discountPercentage" />
<label>Tax1</label>
<input type="text" name="tax1name" class="form-control" id="tax1name" ng-model="p.tax1.name" />
<input type="hidden" name="tax1id" class="form-control" id="tax1id" ng-model="p.tax1.id" />
<input type="hidden" name="tax1value" class="form-control" id="tax1val" ng-model="p.tax1.value" />
<label>Tax2</label>
<input type="text" name="tax2name" class="form-control" id="tax2name" ng-model="p.tax2.name" />
<input type="hidden" name="tax2id" class="form-control" id="tax2id" ng-model="p.tax2.id" />
<input type="hidden" name="tax2value" class="form-control" id="tax2val" ng-model="p.tax2.value" />
<button type="submit" class="form-control btn btn-success" style="margin-top: 10px" ng-click="update(p)">Update</button>
</div>
</div>

files not uploading when there more than 1 seperate file upload fields

Can some tell me why doesnt my selected files are not been passed through AngularJS and captured by PHP?
I am using this plugin to handle https://github.com/danialfarid/ng-file-upload
when a i do a var_dump($_FILES) it always shows as empty
HTML
<form enctype="multipart/form-data" method="post">
<div class="form-group">
<input type="text" class="form-control" ng-model="subject" required>
</div>
<div class="form-group">
<input type="file" class="form-control" ngf-select="" ng-model="cv" required>
</div>
<div class="form-group">
<input type="file" class="form-control" ngf-select="" ng-model="photo" required>
</div>
<div class="form-group">
<input type="file" class="form-control" ngf-select="" ng-model="voice" required>
</div>
<button type="submit" class="btn btn-default" ng-click="sendfiles(cv,photp,voice)">Submit</button>
</form>
JS
angular.module('uploadApp', ['ngFileUpload']);
var appCtrl= [ '$scope', 'Upload', function($scope, Upload) {
$scope.sendfiles= function(file1,file2,file3) {
Upload.upload({
url: 'uploading.php',
method: 'POST',
file: [file1,file2,file3],
data: {
'subject': $scope.subject,
}
})
};
}];

Resources