This repository was archived by the owner on Mar 18, 2022. It is now read-only.
forked from danialfarid/ng-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-file-upload.min.js
6 lines (6 loc) · 2.19 KB
/
angular-file-upload.min.js
1
2
3
4
5
6
/**!
* AngularJS file upload directive and http post
* @author Danial <[email protected]>
* @version 0.1.4
*/
var angularFileUpload=angular.module("angularFileUpload",[]);(function(){if(typeof jQuery==="undefined"){var t=document.createElement("script");t.setAttribute("src","//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");document.getElementsByTagName("head")[0].appendChild(t)}var n="";if(typeof FormData==="undefined"){var t=document.createElement("script");var r=document.getElementsByTagName("script");for(var i=0;i<r.length;i++){var s=r[i].src.indexOf("angular-file-upload.js");if(s==-1){s=r[i].src.indexOf("angular-file-upload.min.js")}if(s>-1){n=r[i].src.substring(0,s);break}}if(typeof FileAPI==="undefined"||FileAPI.staticPath==null){FileAPI={staticPath:n}}t.setAttribute("src",n+"FileAPI.min.js");document.getElementsByTagName("head")[0].appendChild(t)}})();angularFileUpload.directive("ngFileSelect",["$parse","$http",function(e,t){if(t.uploadFile===undefined){t.uploadFile=function(e){if(typeof FormData==="undefined"){e.complete=function(t,n){var r=/^\s*(\[|\{[^\{])/,i=/[\}\]]\s*$/,s=/^\)\]\}',?\n/;var o=n.responseText;if(typeof o=="string"){o=o.replace(s,"");if(r.test(o)&&i.test(o))o=typeof o=="string"?JSON.parse(o):o}if(e.promiseThen!=null)e.promiseThen(o,n.status,null,e);if(e.promiseError!=null)e.promiseError(o,n.status,null,e);if(e.promiseSuccess!=null)e.promiseSuccess(o,n.status,null,e)};if(e.file!=null){e.files={file:e.file}}var n=FileAPI.upload(e);return{then:function(t){e.promiseThen=t},success:function(t){e.promiseSuccess=t},error:function(t){e.promiseError=t}}}else{e.method=e.method||"POST";e.headers=e.headers||{};e.headers["Content-Type"]=false;e.transformRequest=function(t){var n=new FormData;n.append("file",e.file);for(key in e.data){n.append(key,e.data[key])}return n};return t(e)}}}return function(t,n,r){if(typeof FormData==="undefined"){n.wrap('<div class="js-fileapi-wrapper" style="position:relative; overflow:hidden">')}var i=e(r["ngFileSelect"]);n.bind("change",function(e){var n=[];if(typeof FormData!=="undefined"){var r=e.target.files;for(var s=0;s<r.length;s++){n.push(r.item(s))}}else{n=FileAPI.getFiles(e)}t.$apply(function(){i(t,{$files:n,$event:e})})})}}])