angularJs 取<input type='file'>中文件名 (ng-model获取不了)
来源:互联网 发布:剑三代练淘宝下单好吗 编辑:程序博客网 时间:2024/06/05 10:51
用AngularJS写文件上传时遇到了个问题,就是文件类型判断,一般上传文件类型判断都是在前端完成,如果提交给后台发请求判断处理,这无疑会增加服务器端压力。
AngularJS还有点不同的是,这里用的都是双向绑定,但是双向绑定时参数传过去是undefined或者为空。
<div ng-controller="editorProceCtrl" > <input type="file" ng-model="feupload" />{{feupload}} <button ng-click="upsave()" >上传</button></div>js代码
$scope.upsave = function() { alert("@@@@@"+$scope.feupload);//feupload没有值 var fd = new FormData(); var file = document.querySelector('input[type=file]').files[0]; if($scope.feupload.length> 1 && $scope.feupload) { var ldot = filename.lastIndexOf("."); var type = filename.substring(ldot + 1); if(type != "doc" && type !="docx") { alert("文件类型不对,请重新选择"); //清除当前所选文件... }else{ 发送后台操作... } }}直接这样写,后台直接定义取是不行的,ng-model并不能将值传给js
网上找了资料,最后的处理方法:
<div ng-controller="editorProceCtrl" >//onchange改变事件 <input type="file" ng-src={{files[0].name}} onchange='angular.element(this).scope().fileChanged(this)'/> <button ng-click="upsave()" >上传</button></div>js代码:
$scope.fileChanged = function(ele){ $scope.files = ele.files; $scope.$apply(); //传播Model的变化。 } /** * 文件上传 */ $scope.upsave = function() { var fd = new FormData(); var file = document.querySelector('input[type=file]').files[0]; var filename = $scope.files[0].name; if(filename.length> 1 && filename ) { var ldot = filename.lastIndexOf("."); var type = filename.substring(ldot + 1); if(type != "doc" && type !="docx") { alert("文件类型不对,请重新选择"); //清除当前所选文件... }else{ 发送后台操作... } }是的,这样就可以了。
0 0
- angularJs 取<input type='file'>中文件名 (ng-model获取不了)
- angularJs 取<input type='file'>中文件名
- ng-model获取不了type=file值的解决办法
- angularjs上传 和 <input type="file">与ng-model 的玩法
- AngularJS 双向绑定 <input type='file'>中文件名,文件内容
- 关于jsp中<input type="file">获取路径问文件名,获取完全路径问题
- 关于jsp中<input type="file">获取路径问文件名,获取完全路径问题
- IE11下获取<input type="file">的文件名
- jquery获取input type=file的选中上传文件名
- AngularJs ng-model在input中无效,js无法调用
- ionic ng-model controller中取不到值(input)
- ionic ng-model controller中取不到值(input)
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS中获取ng-repeat动态生成的ng-model值
- jquery input file获取文件名
- angularJS <input type="file> 图片的base64编码
- angularjs ng-model绑定number\date类型input框情况
- android中px与sp,dp之间的转换
- 1983 等式问题
- linux学习第一天
- Android N之SurfaceFlinger流程解析(6)
- SqueezeNet运用到Faster RCNN进行目标检测+OHEM
- angularJs 取<input type='file'>中文件名 (ng-model获取不了)
- 归并排序
- 排序学习
- 单例模式
- Bridge 模式
- Simplify Path
- codeforces 743D Chloe and pleasant prizes (DFS/树形DP)
- java多线程(第二种方式)
- 【Android 开发入门】android studio 控制台打印输出日志