Introduction:
Here I will explain how to upload files / images using angularjs example in asp.net or angularjs upload multiple files / images with progress example in asp.net using file upload module or File upload module in angularjs to upload multiple files / images in asp.net using c#, vb.net. In angularjs by using file upload module and html5 property “multiple” we can implement uploading multiple files / images in asp.net.
Description:
In previous articles I explained jQuery upload multiple files in asp.net with handler, jQuery upload multiple files using multiple file upload plugin, jQuery multiple files upload with progress in asp.net, asp.net download multiple files as zip folder with example, 7 + jQuery multiple fileupload plugin examples and many articles relating to angularjs, jQuery, JavaScript and asp.net. Now I will explain how to upload multiple files / images using angularjs with generic handler (ashx) in asp.net using c#, vb.net.
In previous articles I explained jQuery upload multiple files in asp.net with handler, jQuery upload multiple files using multiple file upload plugin, jQuery multiple files upload with progress in asp.net, asp.net download multiple files as zip folder with example, 7 + jQuery multiple fileupload plugin examples and many articles relating to angularjs, jQuery, JavaScript and asp.net. Now I will explain how to upload multiple files / images using angularjs with generic handler (ashx) in asp.net using c#, vb.net.
To upload multiples files using angularjs
in asp.net
we are going to use angularjs
file upload module. First create new web application and open Default.aspx page and write the
following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head data-ng-app="UploadApp">
<title>AngularJS Upload Images using File Upload Plugin</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="scripts/ng-file-upload-shim.min.js"
type="text/javascript"></script>
<script src="scripts/ng-file-upload.min.js"
type="text/javascript"></script>
<script type="text/javascript">
//inject angular file upload
directives and services.
var app = angular.module('fileUpload',
['ngFileUpload']);
app.controller('MyCtrl',
['$scope', 'Upload',
'$timeout', function
($scope, Upload, $timeout) {
$scope.uploadPic = function(file)
{
file.upload = Upload.upload({
url: 'UploadHandler.ashx',
data: {file: file}
});
file.upload.then(function
(response) {
$timeout(function
() {
file.result = response.data;
});
}, 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));
});
}
}]);
</script>
<style type="text/css">
form .progress
{
line-height: 15px;
}
.progress {
display: inline-block;
width: 100px;
border: 3px groove #CCC;
}
.progress div {
font-size: smaller;
background: orange;
width: 0;
}
</style>
</head>
<body ng-app="fileUpload" ng-controller="MyCtrl">
<form name="myForm">
<h3>AngularJS Upload Multiple Files</h3>
Upload:
<input type="file" ngf-select ng-model="picFile"
name="file"
multiple 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>
<br>
<button ng-disabled="!myForm.$valid" ng-click="uploadPic(picFile)">Submit</button><br />
<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>
</form>
</body>
</html>
|
If you observe above code in header section we added
“ng-file-upload-shim.min.js” and “ng-file-upload.min.js” file reference you can get those two files from this link angularjs
file upload module or you can
download from attached folder.
Here we added these references to upload files to
folder and we can implement validation for file upload control and add
restriction to allow only 2MB files to upload by using angularjs
file upload module.
In angularjs
uploadPic method we mentioned “UploadHandler.ashx” by using this we can upload files in server side. Now we will add
handler file in our application by following below steps
Right click on your application à select Add
New Item à select Generic
Handler file à Give a name and click Add button like as shown below
Once you finished adding handler file now add new
folder in your application “uploads” that would be like as shown below
Now open UploadHandler.ashx file and write
the following code
C#
Code
|
VB.NET
Code
|
Now run your application that will allow you to
upload files in upload folder using handler files.
Demo
Download
Sample Code Attached
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
3 comments :
I love you man! God bless you.
good evening sir pls it isnt working for me..
you are great
Note: Only a member of this blog may post a comment.