翻译于 2015/02/24 14:54
如题所示,在这里我将展示一种使用Angular.js和ASP.NET MVC5 来实现上传文件非常简单的方法.
网上已经有很多库实现这个功能了.而我的方法会有什么特别之处呢?如果你已经意识到这个问题了,是非常酷的.思考一下为什么我们会被这个问题一直困扰呢?
我们的要求非常简单,我有一个模型,如下:
public class TutorialModel { public string Title { get; set; } public string Description { get; set; } public HttpPostedFileBase Attachment { get; set; } }
为了实现功能,这里我会用到HTML5的表单数据.我曾经为这个写过一个分布式的angular模块以便让每个人都可以使用.让我们来看看我的akFileUploader模块.
"use strict" angular.module("akFileUploader", []) .factory("akFileUploaderService", ["$q", "$http", function ($q, $http) { var getModelAsFormData = function (data) { var dataAsFormData = new FormData(); angular.forEach(data, function (value, key) { dataAsFormData.append(key, value); }); return dataAsFormData; }; var saveModel = function (data,url) { var deferred = $q.defer(); $http({ url: url, method: "POST", data: getModelAsFormData(data), transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).success(function (result) { deferred.resolve(result); }).error(function (result, status) { deferred.reject(status); }); return deferred.promise; }; return { saveModel: saveModel } }]) .directive("akFileModel", ["$parse", function ($parse) { return { restrict: "A", link: function (scope, element, attrs) { var model = $parse(attrs.akFileModel); var modelSetter = model.assign; element.bind("change", function () { scope.$apply(function () { modelSetter(scope, element[0].files[0]); }); }); } }; }]);
至于这个模块是什么,我会给你一个简短的解释,他有一个directive(指令)和一个Factory.
akFileModel的指令: 他能响应式的为modelSetter改变文件和隐藏文件.
akFileUploader的服务: 它主要是创建一个表单数据和通过$http发送所需的URL.
application.js
"use strict"; (function () { angular.module("application", ["ngRoute", "akFileUploader"]); })();
template
<form class="form-horizontal"> <h4>Tutorial</h4> <hr /> <div class="form-group"> <label for="title" class="col-md-2 control-label">Title</label> <div class="col-md-10"> <input type="text" data-ng-model="tutorial.title" name="title" class="form-control" /> </div> </div> <div class="form-group"> <label for="description" class="col-md-2 control-label">Description</label> <div class="col-md-10"> <textarea data-ng-model="tutorial.description" name="description" class="form-control"> </textarea> </div> </div> <div class="form-group"> <label for="attachment" class="col-md-2 control-label">Attachment</label> <div class="col-md-10"> <input type="file" name="attachment" class="form-control" data-ak-file-model="tutorial.attachment" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="button" class="btn btn-primary" value="Save" data-ng-click="saveTutorial(tutorial)" /> </div> </div> </form>
service:
"use strict"; (function () { angular.module("application") .factory("entityService", ["akFileUploaderService", function (akFileUploaderService) { var saveTutorial = function (tutorial) { return akFileUploaderService.saveModel(tutorial, "/controllerName/actionName"); }; return { saveTutorial: saveTutorial }; }]); })();
controller(js):
"use strict"; (function () { angular.module("application") .controller("homeCtrl", ["$scope", "entityService", function ($scope, entityService) { $scope.saveTutorial = function (tutorial) { entityService.saveTutorial(tutorial) .then(function (data) { console.log(data); }); }; }]); })();
MVC Controller Action:
[HttpPost] public ActionResult SaveTutorial(TutorialModel tutorial) { return Json("Tutorial Saved",JsonRequestBehavior.AllowGet); }
这里我就介绍完了,以上,谢谢
评论删除后,数据将无法恢复
评论(3)
但有两个小疑问:
1.第一个模块里的那个FormData 是什么类. 直接用普通的{}不行么
2.tutorial.attachment 直接用ng-model进行双向绑定不行么,为什么还要额外写一个directive来实现双向绑定
引用来自“_Elvis”的评论
没看明白,是简单了呢,还是复用性好呢,还是?