Getting value null while uploading file through AngularJs in Laravel - angularjs

When I am trying to upload a file using POST Request in Angular Js then I can not able to upload the file because it shows file value null
This is angular part app.js
$scope.submitForm = function(api_url , product){
$http({
method: 'POST',
url: enviroment_url + api_url,
data: product,
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
}).then(function successCallback(response) {
if(response.status == 200){
toaster.pop('success', "Message", response.data.message, 3000, 'trustedHtml');
}
}, function errorCallback(response) {
toaster.pop('error', "Message", response.data.message, 3000, 'trustedHtml');
});
}
form.blade.php
<form id="myform">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
<h4 class="modal-title float-left" id="Heading">Add Product</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name<span class="danger">*</span></label>
<input class="form-control " ng-model="product.name" type="text" placeholder="Enter Product Name">
</div>
<div class="form-group">
<label>Product Image</label>
<input type="file" file-model='product.img_url' class="form-control" id="upload" />
</div>
<div class="form-group">
<label>Description</label>
<textarea rows="2" class="form-control" ng-model="product.description" placeholder="Enter Description"></textarea>
</div>
</div>
<div class="modal-footer ">
<button type="submit" ng-click="submitForm('add-product', product)"
class="btn btn-warning btn-lg " data-dismiss="modal"
style="width: 100%;">
<span class="glyphicon glyphicon-ok-sign"></span>Add
</button>
</div>
</div>
fileController.php
public function addProduct(Request $request){
dd($request->all());
}
OUTPUT the result due to dd() function
array:1 [
"{"name":"Allen","description":"Sint_dolores_ipsam_n","img_url":{}}" => null
]

Your content type in headers should be like this
'Content-Type': 'multipart/form-data'

Related

Uploading images and multiple files in binary data format to SQL Server using ASP.NET with AngularJs

I want to upload images and multiple files in binary data format to SQL Server using ASP.NET with AngularJs, I would be very grateful if you could help me how to do it.
Below is my home controller and my html page with my controller page that I wrote
$scope.Add = function() {
var _Employee = {
"Image": $scope.img,
"CV": $scope.CV
};
$http({
method: 'POST',
url: '/Home/employeeCreate',
data: _Employee
})
public JsonResult employeeCreate(Models.Employee _employee)
{
try
{
db.Employee.Add(_employee);
db.SaveChanges();
return GetEmployee();
}
catch (Exception ex)
{
return Json(ex.Message);
}
}
<div class="form-group col-lg-3">
<div class="profile-images-card">
<div class="profile-images">
<img src="../../img/profile1.jpg" id="upload-img" />
</div>
<div class="custom-file">
<label for="fileupload">Fotograf Yükle</label>
<input type="file" id="fileupload" ng-model="img" />
</div>
</div>
<div>
<label for="formFile" class="form-label">CV</label>
<input class="form-control" type="file" id="formFile" ng-model="CV" />
</div>
<br /> <br />
<div style="float:right">
<input class="btn btn-danger btn-rounded btn-sm" type="button" value="Temizle" ng-click="clearPersonnel()" />
<input class="btn btn-primary btn-rounded btn-sm" type="button" id="add" value="Kaydet" ng-click="Add(myform.$valid)" />
</div>
</div>

Data edit - modal Bootstrap

I have a problem with editing data in the bootstrap modal.
The problem is that in the modal, only first record from the database is displayed.
Modal only displays the problem because it retrieves data from 1 record.
Could it show me something wrong with the code, that it only gets 1 record for me?
My code AngularJS:
controllersAdmin.controller('linkEdit', function( $scope, $http, $routeParams, $timeout){
var linkID = $routeParams.id;
$scope.id = linkID;
$scope.link = function( link ){
$http({
method: 'GET',
url: 'api/admin/links/get/' + linkID,
}).then(function (response){
$scope.link = response.data.records;
},function (error){
console.log('Blad we wczytywaniu danych');
});
}
$scope.saveChanges = function( link ){
$http({
method: 'POST', url: 'api/admin/links/update/', data: {'link' : link}},
).then(function (){
$scope.success = true;
},function (error){
console.log('Blad we wczytywaniu danych');
});
}
});
<tr ng-repeat="link in link">
<td>{{$index+1}}</td>
<td>{{link.links | limitTo:50}}...</td>
<td>{{ link.login }}</td>
<td>{{link.password}}</td>
<td>{{link.date}}</td>
<td>
<button ng-click="deleteLink(link, $index)" class="btn btn-danger">×</button>
<button data-toggle="modal" ng-click="edtlnk(link)" data-target="#edit-data" class="btn btn-primary">Edit</button>
<div class="modal fade" id="edit-data" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="POST" name="editlinke" role="form" ng-submit="saveChanges( link )">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Item</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" class="form-control" ng-model="link.login">
<input type="text" class="form-control" ng-model="link.password">
<input type="text" class="form-control" ng-model="link.links">
</div>
</div>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>

Materializecss form with angularjs not submiting

I am using materializecss and my project uses angularjs as well.I have a form which looks like this.Both html and js are attached.The problem is that when i press submit no data is passed from the form.The relevant add function is called.In console it shows that the variable test is {} after stringify.I am not understanding why.Please help.Any help would be appreciated .(i have defined ng-app in the main file.Havent attached that here)
app.controller("MyAddController", function($scope, $http) {
$scope.test = {};
$scope.add = function() {
console.log("------------>"+JSON.stringify($scope.test));
$http({
url: "rest/leave/create",
method: "POST",
data: JSON.stringify($scope.test),
headers: {'Content-Type': 'application/json'}
}).success(function(data, status, headers, config) {
if (data) {
$scope.data = data;
alert("success");
}
}).error(function(data, status, headers, config) {
alert("error");
})
}
});
<!-- Modal Structure -->
<div id="modal1" class="modal" ng-controller="MyAddController">
<div class="modal-content">
<h4>Apply Leave</h4>
<div class="row">
<form class="col s12">
<div class="row modal-form-row">
<div class="input-field col s6">
<input id=num" type="text" class="validate" ng-bind="test.num">
<label for="num">num</label>
</div>
<div class="input-field col s6">
<input id="ename" type="text" class="validate" ng-bind="test.Title">
<label for="ename">Employee Name</label>
</div>
</div>
<div class="row modal-form-row">
<div class="input-field col s5">
<input id="startDate" type="text" class="validate" value="{{selectionDate.startdate}}" ng-bind="test.StartAt">
</div>
<div class="input-field col s5">
<input id="endDate" type="text" class="validate" value="{{selectionDate.enddate}}" ng-bind="test.EndAt">
</div>
<div class="input-field col s2">
<p>
<input type="checkbox" id="test6" value="yes" ng-bind="isFull"/>
<label for="test6">Half Day</label>
</p>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="description" type="text" class="validate" ng-bind="test.Description">
<label for="description">Description</label>
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button class="btn waves-effect waves-light" type="submit" ng-click="add()" name="action">Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
Change ng-bind to ng-model for two way data binding --- view to controller
and you are also missing quotes in
<input id=num" type="text" class="validate" ng-bind="test.num">
Plunker

Can't Receive MVC ViewBag(always null) property after $http post

I am using MVC 5 with angularjs and SSRS 2008 R2 and just trying to display report posted data by angularjs form and display it in #Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new { Height = 600, Width = 1200, SizeToReportContent = true, frameBorder = "0", })
My MVC Controller is returning this
ViewBag.ReportViewer = reportViewer;
return View(); but my post angularjs function is not getting any ViewBag.REportViewer property on response data.
Please just let me know if I am making any mistake while posting, because always my ViewBag.ReportViewer is null.
Please see (html) for #html.ReportViewer at the bottom and (angularjs $http.post) postIncomingMaterialData for call.
Sorry I pasted most of the code here, but had no choice.
Thanks.
(function() {
'use strict'
angular.module('GAiiNSApp').controller('GFIReportModalCtrl', ['$scope', 'getEmployeeNameFctry', 'getGrvNoFctry', '$http', '$log', '$rootScope', '$filter',
function($scope, getEmployeeNameFctry, getGrvNoFctry, $http, $log, $rootScope, $filter) {
$scope.getGRVNo = function(val) {
return getGrvNoFctry.getGRVNoData('/Stores/StoresAPI/GetGRVNo', val).then(function(res) {
var grvNo = [];
angular.forEach(res.data, function(OBJ) {
grvNo.push((OBJ));
});
return grvNo;
});
};
$scope.getEmployee = function(val) {
return getEmployeeNameFctry.getData('/Stores/StoresAPI/GetEmployee', val).then(function(res) {
var users = [];
angular.forEach(res.data, function(OBJ) {
users.push((OBJ.NAME).trim());
});
return users;
});
};
$scope.SubmitData = function() {
$scope.myobject = {};
$scope.myobject.StartDate = $scope.Date1;
$scope.myobject.EndDate = $scope.Date2;
.employeename = $scope.empName;
$scope.myobject.grvno = $scope.grvNo;
$http({
method: "POST",
url: '/Stores/Materials/IncomingMaterial',
params: {
StartDate: $scope.myobject.StartDate,
EndDate: $scope.myobject.EndDate,
HandedOverTo: $scope.myobject.employeename,
GRVNo: $scope.myobject.grvno,
},
cache: false
});
}
}
]);
})();
(function() {
angular.module('GAiiNSApp').factory('postIncomingMaterialData', ['$http'], function($http) {
$http({
method: "POST",
url: MYURL_URL,
data: myObject,
cache: false
});
});
})();
(function() {
angular.module('GAiiNSApp').factory('getEmployeeNameFctry', ['$http', '$q',
function($http, $q) {
return {
getData: function(url, val) {
return $http({
url: url,
method: "GET",
params: {
nameSearch: val,
}
});
}
}
}
]);
})();
(function() {
'use strict'
angular.module('GAiiNSApp').factory('getGrvNoFctry', ['$http', '$q',
function($http, $q) {
return {
getGRVNoData: function(url, val) {
return $http({
url: url,
method: "GET",
params: {
grv: val,
}
});
}
}
}
]);
})();
#using ReportViewerForMvc; #using System.Drawing #using Microsoft.Reporting.WebForms; #using System.Web.UI.WebControls
<script src="~/Areas/Stores/Controllers/StoresNG/StoresCtrl.js"></script>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12" style="padding: 10px; min-height: 500px; margin-left: 10px;" ng-controller="GFIReportModalCtrl">
<fieldset>
<h3>Report</h3>
<hr />
<form name="MeForm" class="form-inline" novalidate>
<div class=" form-group col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label class="col-xs-5 col-sm-2 col-md-4" for="date1">Start Date:<span style="color:red">*</span>
</label>
<p class="input-group">
<input type="text" id="date1" name="fdate" class="form-control" uib-datepicker-popup="{{$root.format}}" ng-model="$root.Date1" is-open="$root.popup1.opened" datepicker-options="$root.dateOptions1" ng-required="true" close-text="Close" alt-input-formats="$root.altInputFormats"
uib-tooltip="Date must be in dd-mm-yyyy format" tooltip-placement="top-right" tooltip-trigger="'mouseenter'" tooltip-enable="!inputModel" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
<p class="error validationerror" ng-show="MeForm.fdate.$invalid && MeForm.fdate.$touched">Start date is required</p>
</p>
</div>
<div class=" form-group col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label class="col-xs-5 col-sm-2 col-md-4" for="date2">End Date:<span style="color:red">*</span>
</label>
<p class="input-group">
<input type="text" id="date2" name="ldate" class="form-control" uib-datepicker-popup="{{$root.format}}" ng-model="$root.Date2" is-open="$root.popup2.opened" datepicker-options="$root.dateOptions2" ng-required="true" close-text="Close" alt-input-formats="$root.altInputFormats"
uib-tooltip="Date must be in dd-mm-yyyy format" tooltip-placement="top-right" tooltip-trigger="'mouseenter'" tooltip-enable="!inputModel" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
<p class="error validationerror" ng-show="MeForm.ldate.$invalid && MeForm.ldate.$touched">End date is required</p>
</p>
</div>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label class="col-xs-5 col-sm-2 col-md-4" for="HandOver">Handed Over To:</label>
<input type="text" id="HandOver" ng-model="empName" uib-typeahead="name for name in getEmployee($viewValue)" typeahead-loading="loadingName" typeahead-no-results="noResults" typeahead-editable="false" typeahead-min-length="3" typeahead-wait-ms="500" class="form-control">
<i ng-show="loadingName" class="glyphicon glyphicon-refresh"></i>
<div ng-show="noResults">
<i class="glyphicon glyphicon-remove"></i> No Results Found...
</div>
</div>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label class="col-xs-5 col-sm-2 col-md-4" for="GRVNo">GRV No:</label>
<input type="text" id="GRVNo" ng-model="grvNo" uib-typeahead="grvno for grvno in getGRVNo($viewValue)" typeahead-loading="loadingGRVNo" typeahead-no-results="noResults" typeahead-editable="false" typeahead-min-length="3" typeahead-wait-ms="500" class="form-control">
<i ng-show="loadingGRVNo" class="glyphicon glyphicon-refresh"></i>
<div ng-show="noResults">
<i class="glyphicon glyphicon-remove"></i> No Results Found...
</div>
</div>
<div class="row">
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12">
<br />
<br />
<div class="form-group col-xs-3 col-sm-3 col-md-3 col-lg-3 pull-right">
<button type="submit" class="btn submitbtn" ng-click="SubmitData()">Submit</button>
</div>
</div>
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12" style="border: red;">
<div>
<div>
</div>
#{ if (ViewBag.ReportViewer != null) { #Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new { Height = 600, Width = 1200, SizeToReportContent = true, frameBorder = "0", }) } else {
<p>Message -- The ViewBag is still null</p>
} }
</div>
</div>
</form>
</fieldset>
</div>

Angular js Update a div without updating the whole page

Please i want to update a specific div in my view instead of reloading the whole page.
This is my Script.
$scope.submitComment = function() {
$http({
url: "comment",
method: "POST",
data: $.param($scope.commentData),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response) {
console.log(response);
$scope.loading = true;
//location.reload();
$scope.loading = false;
}).error(function(response) {
console.log(response);
alert('Sorry an error occured');
});
};
This is my view
<div ng-controller="myController">
<br> <br> <br> <br>
<form ng-submit="submitComment()" id="form">
<div class="form-group">
<label for="author">Author: </label>
<input id="author" class="form-control " type="text" ng-model="commentData.author" name="author" placeholder="Enter author">
</div>
<div class="form-group">
<label for="message">Message:</label> #{{ commentData.message }}
<textarea id="message" class="form-control" ng-model="commentData.message" name="message" placeholder="Enter Comment" rows="8" cols="40"></textarea>
</div>
<div class="form-group pull-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-comment-o"> Comment</i></button>
<!-- <input class="btn btn-primary" type="submit" value="Comment"> -->
</div>
</form>
<h2>Comments</h2> <hr>
<div ng-repeat="comment in comments">
<div><strong>#{{ comment.author }}</strong></div>
<div>#{{ comment.message }} <button type="button" ng-click="deleteComment(comment.id)" class="btn btn-danger-outline btn-sm" id="submit">Delete</button></div>
<br>
</div>
</div>
I want to update my form so that the text in it goes. and also update my comments as well. I need a help on this please.
In your $scope.submitComment() method, $http.success(), you need to $scope.comments.push($scope.commentData) and you should be good to go.
$scope.submitComment = function() {
$http({
// ...
}).success(function(response) {
$scope.comments.push($scope.commentData);
});
};

Resources