angularjs中$http异步上传Excel文件方法
更新时间:2018年02月23日 14:04:34 作者:码农苏
本篇文章给大家详细分析了angularjs中$http异步上传Excel文件方法,对此有需要的读者可以学习下。
1.文件上传框html代码如下
<form id="fileForm" enctype="multipart/form-data"> <button id="import_asset" type="button" ng-click="import_asset()">上传文件</button> <input id="file_asset" type="file" style="display: none;"/> </form>
*注意: 设置form的enctype属性值为:multipart/form-data
2:js代码如下:
$scope.import_asset = function () { $("#file_asset").click(); }; $("#file_asset").on("change", function(){ var formData = new FormData(); var file = document.getElementById("file_asset").files[0]; if(file.name){ var fileName = file.name.substring(file.name.lastIndexOf(".") + 1); if(fileName =="xlsx" || fileName =="xls"){ formData.append('file', file); $http({ method:"post", url:commonService.projectName + "/so/assetmanage/upload", data:formData, headers : { 'Content-Type' : undefined }, transformRequest : angular.identity }).then(function (response) { if(response.status == 200){ alert("文件上传成功!!!"); }else{ alert("文件上传失败!!!"); } }); }else{ alert("文件格式不正确,请上传以.xlsx,.xls 为后缀名的文件。"); $("#file_asset").val(""); } } });
相关文章
Angular.js中window.onload(),$(document).ready()的写法浅析
这篇文章主要介绍了Angular.js中window.onload(),$(document).ready()的写法浅析,需要的朋友可以参考下2017-09-09Angular项目中$scope.$apply()方法的使用详解
这篇文章主要给大家介绍了关于Angular项目中$scope.$apply()方法使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Angularjs具有一定的参考学习价值,需要的朋友们下面跟着小编一起来看看吧。2017-07-07AngularJs unit-testing(单元测试)详解
本文主要介绍AngularJs unit-testing(单元测试)的内容,这里整理了单元测试的知识,及示例代码,有兴趣的朋友可以参考下2016-09-09
最新评论