As I am new to AngularJS, I Don't know how to upload file in MVC using AngularJS.
I am trying to upload some files without any specific type or extension, but failed.
I created a javascript file which have-
Here is serviceJS-
var app = angular.module("app", []);
app.service('FileUploadService', ['$http', function ($http) {
this.uploadFileToUrl = function (file, uploadUrl) {
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.success(function () {
})
.error(function () {
});
}
}]);
This is controller part-
app.controller('FileUploadController', ['$scope', 'FileUploadService', function($scope, FileUploadService) {
$scope.uploadFile = function () {
var file = $scope.myFile;
console.log('file is ');
console.dir(file);
var uploadUrl = "/Home/FileUploadFromAngular";
FileUploadService.uploadFileToUrl(file, uploadUrl);
};
}]);
And, In view page,
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/AngScript/FileUpload.js"></script>
<script src="~/Scripts/angular-ui/ui-bootstrap.min.js"></script>
<div ng-controller="FileUploadController">
<input type="file" ng-model="myFile" />
<button ng-click="uploadFile()">Upload</button>
</div>
It is taking myFile as undefined in controller. I am unable to debug this.
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…