前面一系列咱們純粹是講AngularJS,在講一門知識時咱們應該結合以前所學綜合起來來作一個小的例子,前面咱們講了在MVC中上傳文件的例子,在本節咱們講講如何利用AngularJS在WebAPi中如何上傳,來鞏固下WebAPi並結合AngularJS中對應的一些組件學習下。css
(一)在WebAPi中咱們如何得到上傳本地文件的物理路徑呢?須要實現此類: MultipartFormDataStreamProvider ,從該類中獲取上傳文件的物理路徑並返回。以下:html
public class UploadMultipartFormProvider : MultipartFormDataStreamProvider { public UploadMultipartFormProvider(string rootPath) : base(rootPath) { } public override string GetLocalFileName(HttpContentHeaders headers) { if (headers != null && headers.ContentDisposition != null) { return headers .ContentDisposition .FileName.TrimEnd('"').TrimStart('"'); } return base.GetLocalFileName(headers); } }
(二)爲避免有些瀏覽器不支持多個文件上傳咱們經過實現 ActionFilterAttribute 特性來加以判斷,以下:jquery
public class MimeMultipart : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (!actionContext.Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException( new HttpResponseMessage( HttpStatusCode.UnsupportedMediaType) ); } } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { } }
以上兩個類爲咱們在WebAPi中上傳提供一個保障。接下來咱們須要的是如何利用AngularJS來上傳。git
在AngularJS中上傳須要用到 ng-file-upload 組件,而非 angularFileUpload 組件,在此以前一直是利用angularFileUpload組件,可是後來逐步遷移而且命名爲ng-file-upload組件。同時爲了一些加載效果如在github中頂部有加載線的過程,決定也在這裏實現一下看看【說明:上傳過程用到angular-loading-bar(加載)和ng-file-upload(上傳)組件】。github
(1)爲了重用,咱們將加載組件進行封裝。bootstrap
//common.load.js
(function () { 'use strict'; angular .module('common.load', [ 'angular-loading-bar', 'ngAnimate' ]); })();
(2)啓動加載線。api
//loadBarCtrl.js
(function (app) { "use strict"; app.controller("loadCtrl", loadCtrl); function loadCtrl(cfpLoadingBar) { cfpLoadingBar.start(); } })(angular.module("common.load"));
(3)上傳文件代碼瀏覽器
//fileUploadCtrl.js
(function (app) { 'use strict'; app.controller('fileUploadCtrl', fileUploadCtrl); fileUploadCtrl.$inject = ['$scope', '$http', '$timeout', 'Upload']; function fileUploadCtrl($scope, $http, $timeout, Upload, cfpLoadingBar) { $scope.upload = []; $scope.UploadedFiles = []; $scope.startUploading = function ($files) { for (var i = 0; i < $files.length; i++) { var $file = $files[i]; (function (index) { $scope.upload[index] = Upload.upload({ url: "/api/upload", method: "POST", file: $file, withCredentials: false }).progress(function (evt) { }).success(function (data, status, headers, config) { $scope.UploadedFiles.push({ FileName: data.FileName, FilePath: data.LocalFilePath, FileLength: data.FileLength }); cfpLoadingBar.complete(); }).error(function (data, status, headers, config) { }); })(i); } } } })(angular.module("common.load"));
(4)加載主模塊以及依賴模塊。app
//app.js
(function () { 'use strict'; angular .module('angularUploadApp', [ 'ngRoute', 'ngFileUpload', 'common.load' ]) .config(config) config.$inject = ['$routeProvider']; function config($routeProvider) { $routeProvider .when('/', { templateUrl: '../../app/templates/fileUpload.html', controller: 'fileUploadCtrl' }) .otherwise({ redirectTo: '/' }); } })();
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link href="~/Content/bootstrap.min.css" rel="stylesheet" /> <link href="~/Content/loading-bar.min.css" rel="stylesheet" media="all" /> </head> <body ng-app="angularUploadApp" ng-controller="loadCtrl"> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="http://www.cnblogs.com/createmyself">Angular File WebAPi Upload by <i style="color:#2da12d;font-weight:bolder;">xpy0928</i></a> </div> <div id="navbar" class="navbar-collapse collapse"> </div> </nav> <div class="container"> <div ng-include src="'../../app/templates/fileUpload.html'"></div> </div> <script src="../../Scripts/jquery-1.10.2.min.js"></script> <script src="../../Scripts/angular.min.js"></script> <script src="../../Scripts/angular-animate.min.js"></script> <script src="../../Scripts/angular-route.js"></script> <script src="../../app/plugins/loading-bar.min.js"></script> <script src="../../app/modules/common.load.js"></script> <script src="../../Scripts/ng-file-upload.min.js"></script> <script src="../../app/app.js"></script> <script src="../../app/controllers/loadBarCtrl.js"></script> <script src="../../app/controllers/fileUploadCtrl.js"></script> </body> </html>
模板頁:async
<div class="row" ng-controller="fileUploadCtrl"> <div class="col-xs-3"> <div> <input type="file" accept="images/*" ngf-pattern="'.png,.jpg,.gif'" ngf-select="startUploading($files)" multiple> </div> </div> <div class="col-xs-9"> <div class="panel-body"> <div class="panel panel-default" ng-repeat="uploadedFile in UploadedFiles track by $index"> <div class="panel-heading"> <strong>{{uploadedFile.FileName}}</strong> </div> <div class="panel-body"> <div class=" media"> <a class="pull-left" href="#"> <img class="media-object" width="100" ng-src="../uploadimages/{{uploadedFile.FileName}}" /> </a> <div class="media-body"> <div class="lead" style="font-size:14px;color: crimson;width:500px;word-wrap:break-word">{{uploadedFile.FilePath}}</div> </div> </div> </div> <div class="panel-footer"> 圖片總字節: <span style="color:black">{{uploadedFile.FileLength}}</span> </div> </div> </div> </div> </div>
後臺api服務:
[RoutePrefix("api/upload")] public class FileUploadController : ApiController { [Route("")] [MimeMultipart] public async Task<FileUploadResult> Post() { var uploadPath = HttpRuntime.AppDomainAppPath + "UploadImages"; if (!Directory.Exists(uploadPath)) Directory.CreateDirectory(uploadPath); var multipartFormDataStreamProvider = new UploadMultipartFormProvider(uploadPath); await Request.Content.ReadAsMultipartAsync(multipartFormDataStreamProvider); string _localFileName = multipartFormDataStreamProvider .FileData.Select(multiPartData => multiPartData.LocalFileName).FirstOrDefault(); return new FileUploadResult { LocalFilePath = _localFileName, FileName = Path.GetFileName(_localFileName), FileLength = new FileInfo(_localFileName).Length }; } }
首先咱們來看看例子搭建的結構:
生成界面效果:
下面咱們來演示下最終效果:
(1)在WebAPi中用到路由特性時,若在控制器中如 [RoutePrefix("api/upload")] 此時在方法中若未有 [Route("")] 此時在上傳的url必須顯示添加Post如: url: "/api/upload/post" 若添加則不用顯示添加方法名。
(2)在angular中加載模板爲
<div ng-include src="''"></div> 【注】:src中要加單引號,不然出錯 或者 <div ng-include></div>
(3)對於在WebAPi中上傳能夠參看此連接,更加詳細。
WebAPi:https://github.com/stewartm83/angular-fileupload-sample
對於AngularJS組件中的加載和上傳還有更多用法,能夠參看以下連接:
ng-file-upload:https://github.com/danialfarid/ng-file-upload
angular-loading-bar:https://github.com/chieffancypants/angular-loading-bar