基于HTML Form直接定义、Submit
有个问题:Action带的参数不能是变量
HTML中定义 Form,通过js函数自行submit
表单提交的AngularJS函数片段:
var formdetailImg = document.getElementById("formdetailImg", AlertUtil.defaultTimeout);
formdetailImg.action = CommonConfig.RestBaseUrl.CaoBaoService + 'goods/detailimgs/add/' + $scope.goodsDetailInfo.id + '/' + $scope.detailImage.title + '/' + $scope.detailImage.sortNum;
formdetailImg.target = "tg"; //tg为隐藏的iframe,为了上传后页面留在原地(不跳转)
formdetailImg.submit();
也有问题:这是异步提交动作,无法立即刷新修改后的数据。
通过AngularJs上传(这个好,自由,想干啥干啥):
$scope.saveGoodsDetailImg = function () { var fd = new FormData(); var file = document.getElementById('goodsDetailImg').files[0]; fd.append('goodsDetailImg', file); $http.post(CommonConfig.RestBaseUrl.CaoBaoService + 'goods/detailimgs/add/' + $scope.goodsDetailInfo.id + '/' + $scope.detailImage.title + '/' + $scope.detailImage.sortNum, fd, CommonConfig.fileUploadReqConfig) .then(function (result) { //正确请求成功时处理 console.log('saveGoodsDetailImg:result', result); if (result.data.code != 'NONE_ERROR') { AlertUtil.dangerWithCloseButton('alertInfo', '修改失败!', AlertUtil.defaultTimeout); return; } $scope.getGoodsDetailImg($scope.goodsDetailInfo.id); }).catch(function (result) { //捕捉错误处理 console.log(result); }); };
相关的配置:
CommonConfig.fileUploadReqConfig = {
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
};