[Angularjs]ng-file-upload上傳文件

寫在前面

最近在弄文檔庫的H5版,就查找了下相關的上傳組件,發現了ng-upload的東東,推薦給你們。html

系列文章

[Angularjs]ng-select和ng-optionshtml5

[Angularjs]ng-show和ng-hidegit

[Angularjs]視圖和路由(一)angularjs

[Angularjs]視圖和路由(二)github

[Angularjs]視圖和路由(三)json

[Angularjs]視圖和路由(四)瀏覽器

[Angularjs]ng-class,ng-class-even,ng-class-oddapp

[Angularjs]單頁應用之分頁ide

[Angularjs]國際化工具

[Angularjs]ng-repeat中使用ng-model遇到的問題

[Angularjs]過濾器

[Angularjs]ng-file-upload上傳文件

ng-file-upload

angular-file-upload 是一款輕量級的 AngularJS 文件上傳工具,爲不支持瀏覽器的 FileAPI polyfill 設計,使用 HTML5 直接進行文件上傳。

特性

  • 支持上傳進度,在上傳的時候,能夠取消或者停止,支持文件拖拽(HTML5),目錄拖拽(weikit),CORS,PUT(html5)/POST 方法

  • 支持使用 Flash polyfill FileAPI  跨瀏覽器上傳 (HTML5 和 non-HTML5) 。容許客戶端在上傳以前驗證或者修改文件。

  • 當文件的內容類型使用 $upload.http()時,支持直接上傳到 CouchDB,imgur 等等。支持 Angular httpPOST/PUT 請求的進度事件,更多內容請看 #88(comment) 

  • Separate shim file loaded on demand for non-HTML5 code meaning no extra load/code if you just need HTML5 support. (Note that html5-shim.js is still needed for progress event in HTML5 browsers)

  • 輕量級,使用常規的 $http 來上傳(支持非 HTML5 瀏覽器),因此提供全部 Angular $http 功能。

一個例子

須要的js文件,能夠去這裏下載:https://github.com/danialfarid/ng-file-upload

<!DOCTYPE html>
<html ng-app="app">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>文件上傳</title>
    <meta charset="utf-8" />
    <script src="JS/angular.min.js"></script>
    <script src="JS/ng-file-upload.min.js"></script>
    <script src="JS/ng-file-upload-shim.min.js"></script>
    <script>
        var app = angular.module('app', ['ngFileUpload']);
        app.controller('FileController', function ($scope, Upload) {
$scope.uploadImg = '';
//提交 $scope.submit = function () { $scope.upload($scope.file); }; $scope.upload = function (file) { $scope.fileInfo = file; Upload.upload({ //服務端接收 url: 'Ashx/UploadFile.ashx', //上傳的同時帶的參數 data: { 'username': $scope.username }, file: file }).progress(function (evt) { //進度條 var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); console.log('progess:' + progressPercentage + '%' + evt.config.file.name); }).success(function (data, status, headers, config) { //上傳成功 console.log('file ' + config.file.name + 'uploaded. Response: ' + data); $scope.uploadImg = data; }).error(function (data, status, headers, config) { //上傳失敗 console.log('error status: ' + status); }); }; }); </script> </head> <body> <form ng-controller="FileController"> <img src="{{uploadImg}}"/>
當前上傳用戶:
<input type="text" placeholder="請輸入您的名稱" name="name" ng-model="username"/><div class="button" ngf-select ng-model="file" name="file" ngf-pattern="'image/*" accept="image/*" ngf-max-size="20MB" ngf-min-height="100">Select</div><button type="submit" ng-click="submit()">submit</button> {{fileInfo.name}}<br /> {{fileInfo.size}} </form></body></html>

簡單測試

其中data中存的爲咱們上傳文件的同時,須要的參數。

完整的例子,上傳成功並在頁面上進行預覽。

public class UploadFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            var paras = context.Request.Params["data"];
            JObject jobj = JObject.Parse(paras);
            string strUserName = jobj["username"].ToString();
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                var file = files[0];
                string fileExt = Path.GetExtension(file.FileName);
                string fileNewName = Guid.NewGuid() + fileExt;
                string strRelativeDir = "/Upload/" + strUserName;
                string strDir = context.Request.MapPath(strRelativeDir);
                if (!Directory.Exists(strDir))
                {
                    Directory.CreateDirectory(strDir);
                }
                string strSavePath = Path.Combine(strDir, fileNewName);
                file.SaveAs(strSavePath);
                context.Response.Write(Path.Combine(strRelativeDir, fileNewName));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

總結

使用ng-file-upload能夠很好的與angularjs結合。在使用的時候,查找了一下angularjs相關的文件上傳的例子,若是瀏覽器支持html5,那也能夠很方便的製做進度條,另外該組件也支持多文件上傳。推薦給你們。

你能夠去這裏進行下載:https://github.com/danialfarid/ng-file-upload

相關文章
相關標籤/搜索