I am developing a file upload module in AngularJS. I will be having multiple file controls and each control can upload one file at a time (not multiple). Finally on click of submit I am saving all the files. I am dynamically generating file controls using ng-repeat as below.
<div class="upload-button" ng-repeat="fileInput in fileInputs">
<div class="upload-button-icon">
<img src="images/folder-small.png">
<div class="upload-text">{{fileInput}}</div>
<input type="file" file-data="{{fileInput}}" file-model="{{fileInput}}" />
</div>
</div>
JS code to assign values to file controls
$scope.fileInputs = ["Passport","Visa"];
Below is my code to upload files.
myapp.factory('fileUpload', ['$http', function ($http) {
var fileuploadurl = "http://localhost:19144/" + 'api/Customer/UploadLeaseFiles/' + 2;
var service = {
uploadUrl: fileuploadurl,
pendingFiles: [],
doUpload: doUpload
};
return service;
function doUpload() {
debugger;
var files = new FormData();
angular.forEach(this.pendingFiles, function (value, index) {
files.append('file', value);
files.append('IndexValue', index);
});
return $http.post(this.uploadUrl, files, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
})
}
}]);
This is my directive code.
myapp.directive('fileModel', ['fileUpload', function (fileUpload) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind("change", function (evt) {
fileUpload.pendingFiles[attrs.fileModel] = evt.target.files[0];
});
}
};
}]);
Above code will work only if I assign integers to fileInputs as:
$scope.fileInputs = [1,2,3];
I am struggling to understand why it is working if I assign integer to fileinputs. How can I assign strings and make the above code work?
pendingFiles. Where are you defining it ?fileUploadinjected in to the directive