[Angularjs]ng-file-upload上传文件

来源:互联网 发布:java swing 表格 编辑:程序博客网 时间:2024/04/18 16:33

ng-file-upload

angular-file-upload 是一款轻量级的 AngularJS 文件上传工具,为不支持浏览器的 FileAPI polyfill 设计,使用 HTML5 直接进行文件上传。

特性

  • 支持上传进度,在上传的时候,可以取消或者中止,支持文件拖拽(HTML5),目录拖拽(weikit),CORS,PUT(html5)/POST 方法

  • 支持使用 Flash polyfill FileAPI  跨浏览器上传 (HTML5 和 non-HTML5) 。允许客户端在上传之前验证或者修改文件。

  • 当文件的内容类型使用 $upload.http()时,支持直接上传到 CouchDB,imgur 等等。支持 Angular httpPOST/PUT 请求的进度事件,更多内容请看 #88(comment) 

  • Separate shim file loaded on demand for non-HTML5 code meaning no extra load/code if you just need HTML5 support. (Note that html5-shim.js is still needed for progress event in HTML5 browsers)

  • 轻量级,使用常规的 $http 来上传(支持非 HTML5 浏览器),所以提供所有 Angular $http 功能。

一个例子

需要的js文件,可以去这里下载:https://github.com/danialfarid/ng-file-upload

复制代码
<!DOCTYPE html><html ng-app="app"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>文件上传</title>    <meta charset="utf-8" />    <script src="JS/angular.min.js"></script>    <script src="JS/ng-file-upload.min.js"></script>    <script src="JS/ng-file-upload-shim.min.js"></script>    <script>        var app = angular.module('app', ['ngFileUpload']);        app.controller('FileController', function ($scope, Upload) {
$scope.uploadImg = '';
//提交 $scope.submit = function () { $scope.upload($scope.file); }; $scope.upload = function (file) { $scope.fileInfo = file; Upload.upload({ //服务端接收 url: 'Ashx/UploadFile.ashx', //上传的同时带的参数 data: { 'username': $scope.username }, file: file }).progress(function (evt) { //进度条 var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); console.log('progess:' + progressPercentage + '%' + evt.config.file.name); }).success(function (data, status, headers, config) { //上传成功 console.log('file ' + config.file.name + 'uploaded. Response: ' + data); $scope.uploadImg = data; }).error(function (data, status, headers, config) { //上传失败 console.log('error status: ' + status); }); }; }); </script></head><body> <form ng-controller="FileController"> <img src="{{uploadImg}}"/>
当前上传用户:
<input type="text" placeholder="请输入您的名称" name="name" ng-model="username"/><div class="button" ngf-select ng-model="file" name="file" ngf-pattern="'image/*" accept="image/*" ngf-max-size="20MB" ngf-min-height="100">Select</div><button type="submit" ng-click="submit()">submit</button> {{fileInfo.name}}<br /> {{fileInfo.size}} </form></body></html>
复制代码

简单测试

其中data中存的为我们上传文件的同时,需要的参数。

完整的例子,上传成功并在页面上进行预览。

复制代码
public class UploadFile : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "application/json";            var paras = context.Request.Params["data"];            JObject jobj = JObject.Parse(paras);            string strUserName = jobj["username"].ToString();            HttpFileCollection files = context.Request.Files;            if (files.Count > 0)            {                var file = files[0];                string fileExt = Path.GetExtension(file.FileName);                string fileNewName = Guid.NewGuid() + fileExt;                string strRelativeDir = "/Upload/" + strUserName;                string strDir = context.Request.MapPath(strRelativeDir);                if (!Directory.Exists(strDir))                {                    Directory.CreateDirectory(strDir);                }                string strSavePath = Path.Combine(strDir, fileNewName);                file.SaveAs(strSavePath);                context.Response.Write(Path.Combine(strRelativeDir, fileNewName));            }        }        public bool IsReusable        {            get            {                return false;            }        }    }
复制代码

 

总结

使用ng-file-upload可以很好的与angularjs结合。在使用的时候,查找了一下angularjs相关的文件上传的例子,如果浏览器支持html5,那也可以很方便的制作进度条,另外该组件也支持多文件上传。推荐给大家。

你可以去这里进行下载:https://github.com/danialfarid/ng-file-upload

0 0
原创粉丝点击