<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(""); } } });原文链接:https://www.f2er.com/angularjs/145070.html