diff --git a/ng-upload.js b/ng-upload.js index 29d5e8d..1fec8a5 100644 --- a/ng-upload.js +++ b/ng-upload.js @@ -58,8 +58,8 @@ angular.module('ngUpload', []) } }; }]) - .directive('ngUpload', ["$log", "$parse", "$document", - function ($log, $parse, $document) { + .directive('ngUpload', ["$log", "$parse", "$document", "$browser", "$http", + function ($log, $parse, $document, $browser, $http) { var iframeID = 1; // Utility function to get meta tag with a given name attribute function getMetaTagWithName(name) { @@ -75,6 +75,10 @@ angular.module('ngUpload', []) return angular.element(match); } + function getCsrfTokenValue() { + return $browser.cookies()[$http.defaults.xsrfCookieName || 'X-XSRF-TOKEN']; + } + return { restrict: 'AC', link: function (scope, element, attrs) { @@ -110,6 +114,11 @@ angular.module('ngUpload', []) options.beforeSubmit = $parse(attrs.uploadOptionsBeforeSubmit); } + if ( attrs.hasOwnProperty( "uploadOptionsEnableCsrf" ) ) { + // allow for blank or true + options.enableCsrf = attrs.uploadOptionsEnableCsrf != "false"; + } + element.attr({ 'target': 'upload-iframe-' + iframeID, 'method': 'post', @@ -133,6 +142,17 @@ angular.module('ngUpload', []) element.append(input); } + + if ( options.enableCsrf ) { + var input = angular.element(""); + input.attr("class", "upload-csrf-token"); + input.attr("type", "hidden"); + input.attr("name", attrs.uploadOptionsCsrfParam || 'CSRFToken'); + input.val(getCsrfTokenValue()); + + element.append(input); + } + element.after(iframe); setLoadingState(false); diff --git a/ng-upload.min.js b/ng-upload.min.js index 92fc2a5..e87b194 100644 --- a/ng-upload.min.js +++ b/ng-upload.min.js @@ -1 +1 @@ -angular.module("ngUpload",[]).directive("uploadSubmit",["$parse",function(){function n(t,e){t=angular.element(t);var a=t.parent();return e=e.toLowerCase(),a&&a[0].tagName.toLowerCase()===e?a:a?n(a,e):null}return{restrict:"AC",link:function(t,e){e.bind("click",function(t){if(t&&(t.preventDefault(),t.stopPropagation()),!e.attr("disabled")){var a=n(e,"form");a.triggerHandler("submit"),a[0].submit()}})}}}]).directive("ngUpload",["$log","$parse","$document",function(n,t,e){function a(n){var t,a=e.find("head");return angular.forEach(a.find("meta"),function(e){e.getAttribute("name")===n&&(t=e)}),angular.element(t)}var r=1;return{restrict:"AC",link:function(e,o,i){function l(n){e.$isUploading=n}function p(){c.unbind("load"),e.$$phase?l(!1):e.$apply(function(){l(!1)});try{var t,a=(c[0].contentDocument||c[0].contentWindow.document).body;try{t=angular.fromJson(a.innerText||a.textContent),e.$$phase?d(e,{content:t}):e.$apply(function(){d(e,{content:t})})}catch(r){t=a.innerHTML;var o="ng-upload: Response is not valid JSON";n.warn(o),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}catch(o){n.warn("ng-upload: Server error"),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}r++;var u={},d=i.ngUpload?t(i.ngUpload):null,f=i.errorCatcher?t(i.errorCatcher):null,s=i.ngUploadLoading?t(i.ngUploadLoading):null;i.hasOwnProperty("uploadOptionsConvertHidden")&&(u.convertHidden="false"!=i.uploadOptionsConvertHidden),i.hasOwnProperty("uploadOptionsEnableRailsCsrf")&&(u.enableRailsCsrf="false"!=i.uploadOptionsEnableRailsCsrf),i.hasOwnProperty("uploadOptionsBeforeSubmit")&&(u.beforeSubmit=t(i.uploadOptionsBeforeSubmit)),o.attr({target:"upload-iframe-"+r,method:"post",enctype:"multipart/form-data",encoding:"multipart/form-data"});var c=angular.element('