[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 forprogress
event inHTML5
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
- [Angularjs]ng-file-upload上传文件
- AngularJS 文件上传控件 ng-file-upload
- AngularJS 文件上传控件 ng-file-upload
- [Angularjs]ng-file-upload上传文件
- AngularJS 文件上传控件 ng-file-upload
- angularJs中上传图片/文件功能:ng-file-upload
- angularjs 文件上传ng-file-upload插件使用
- angularjs之ng-file-upload上传头像
- angularjs之ng file upload
- angular-file-upload angularJS 文件上传
- AngularJS下$http上传文件(AngularJS file upload/post file)
- RAP upload file 上传文件
- angular file upload 文件上传
- JQuery Upload File 上传文件提示 Empty file upload result
- angular js 上传插件 angular-file-upload 和 ng-file-upload 使用时注意事项
- Spring MVC 文件上传 file upload
- SpringMVC File Upload 多文件上传
- jquery-file-upload 文件上传,带进度条
- Android自定义view详解
- ListView的异步加载图片乱序
- 25javascript的prototype
- linux 下git服务器
- 如何打开无线网卡开关?
- [Angularjs]ng-file-upload上传文件
- Java操纵postgresql
- UVa11181 Probability|Given DFS+概率
- 【Android】根据宽度自动调整(参照图片本身尺寸)高度
- 手机页面的字体问题
- 26javascript基于对象
- 可左右上下滑动切换图片的界面
- 简陋hash算法
- mysql sql语句的用法