Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
danialfarid committed Mar 24, 2014
1 parent d21c267 commit 5a32e27
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Thumbs.db
/target/*
classes/
._*
/node_modules
/node_modules
*.jar
2 changes: 1 addition & 1 deletion demo/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/commons-fileupload-1.3.jar"/>
<classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
<classpathentry kind="output" path="war/WEB-INF/classes"/>
</classpath>
Binary file modified demo/war/WEB-INF/lib/appengine-api-labs.jar
Binary file not shown.
Binary file modified demo/war/WEB-INF/lib/appengine-endpoints.jar
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion demo/war/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ <h3>
</ul>
</div>
</div>
<div style="position:absolute;bottom:10px;right:10px;font-size:smaller;color:#777">Last update: 2014-02-19</div>
<div style="position:absolute;bottom:10px;right:10px;font-size:smaller;color:#777">Last update: 2014-03-24</div>
</body>
</html>
18 changes: 10 additions & 8 deletions demo/war/js/angular-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ if (window.XMLHttpRequest) {
xhr.__requestHeaders = [];
xhr.open = (function(orig) {
if (!xhr.upload) xhr.upload = {};
xhr.__listeners = [];
xhr.upload.addEventListener = function(t, fn, b) {
if (t === 'progress') {
xhr.__progress = fn;
}
if (t === 'load') {
xhr.__load = fn;
}
xhr.__listeners[t] = fn;
};
return function(m, url, b) {
orig.apply(xhr, [m, url, b]);
Expand Down Expand Up @@ -93,7 +89,12 @@ if (window.XMLHttpRequest) {
var config = {
url: xhr.__url,
complete: function(err, fileApiXHR) {
if (!err) xhr.__load({type: 'load', loaded: xhr.__total, total: xhr.__total, target: xhr, lengthComputable: true});
if (!err && xhr.__listeners['load'])
xhr.__listeners['load']({type: 'load', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (!err && xhr.__listeners['loadend'])
xhr.__listeners['loadend']({type: 'loadend', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (err === 'abort' && xhr.__listeners['abort'])
xhr.__listeners['abort']({type: 'abort', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (fileApiXHR.status !== undefined) Object.defineProperty(xhr, 'status', {get: function() {return fileApiXHR.status}});
if (fileApiXHR.statusText !== undefined) Object.defineProperty(xhr, 'statusText', {get: function() {return fileApiXHR.statusText}});
Object.defineProperty(xhr, 'readyState', {get: function() {return 4}});
Expand All @@ -104,8 +105,9 @@ if (window.XMLHttpRequest) {
},
progress: function(e) {
e.target = xhr;
xhr.__progress(e);
xhr.__listeners['progress'] && xhr.__listeners['progress'](e);
xhr.__total = e.total;
xhr.__loaded = e.loaded;
},
headers: xhr.__requestHeaders
}
Expand Down
87 changes: 52 additions & 35 deletions demo/war/js/angular-file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ angularFileUpload.service('$upload', ['$http', '$timeout', function($http, $time
function sendHttp(config) {
config.method = config.method || 'POST';
config.headers = config.headers || {};
config.transformRequest = config.transformRequest || function(data) {
if (window.ArrayBuffer && data instanceof ArrayBuffer) {
config.transformRequest = config.transformRequest || function(data, headersGetter) {
if (window.ArrayBuffer && data instanceof window.ArrayBuffer) {
return data;
}
return $http.defaults.transformRequest[0](data);
return $http.defaults.transformRequest[0](data, headersGetter);
};

if (window.XMLHttpRequest.__isShim) {
config.headers['__setXHR_'] = function() {
return function(xhr) {
config.__XHR = xhr;
config.xhrFn && config.xhrFn(xhr);
xhr.upload.addEventListener('progress', function(e) {
if (config.progress) {
$timeout(function() {
Expand All @@ -40,76 +41,92 @@ angularFileUpload.service('$upload', ['$http', '$timeout', function($http, $time
}
};
}

var promise = $http(config);

promise.progress = function(fn) {
config.progress = fn;
return promise;
};
};
promise.abort = function() {
if (config.__XHR) {
$timeout(function() {
config.__XHR.abort();
});
}
return promise;
};
};
promise.xhr = function(fn) {
config.xhrFn = fn;
return promise;
};
promise.then = (function(promise, origThen) {
return function(s, e, p) {
config.progress = p || config.progress;
var result = origThen.apply(promise, [s, e, p]);
result.abort = promise.abort;
result.progress = promise.progress;
result.xhr = promise.xhr;
return result;
};
})(promise, promise.then);

return promise;
}

this.upload = function(config) {
config.headers = config.headers || {};
config.headers['Content-Type'] = undefined;
config.transformRequest = config.transformRequest || $http.defaults.transformRequest;
var formData = new FormData();
var i;
if (config.data) {
for (var key in config.data) {
var val = config.data[key];
if (!config.formDataAppender) {
if (typeof config.transformRequest == 'function') {
val = config.transformRequest(val);
} else {
for (i = 0; i < config.transformRequest.length; i++) {
var fn = config.transformRequest[i];
if (typeof fn == 'function') {
val = fn(val);
if (config.formDataAppender) {
for (var key in config.data) {
var val = config.data[key];
config.formDataAppender(formData, key, val);
}
config.transformRequest = angular.identity;
} else {
var origTransformRequest = config.transformRequest;
var origData = config.data;
config.transformRequest = function(formData, headerGetter) {
for (var key in origData) {
var val = origData[key];
if (typeof origTransformRequest == 'function') {
val = origTransformRequest(val, headerGetter);
} else {
for (var i = 0; i < origTransformRequest.length; i++) {
var transformFn = origTransformRequest[i];
if (typeof transformFn == 'function') {
val = transformFn(val, headerGetter);
}
}
}
formData.append(key, val);
}
formData.append(key, val);
} else {
config.formDataAppender(formData, key, val);
}
return formData;
};
}
}
config.transformRequest = angular.identity;

var fileFormName = config.fileFormDataName || 'file';

if (Object.prototype.toString.call(config.file) === '[object Array]') {
var isFileFormNameString = Object.prototype.toString.call(fileFormName) === '[object String]';
for (i = 0; i < config.file.length; i++) {
formData.append(isFileFormNameString ? fileFormName + i : fileFormName[i], config.file[i], config.file[i].name);

if (config.file != null) {
var fileFormName = config.fileFormDataName || 'file';

if (Object.prototype.toString.call(config.file) === '[object Array]') {
var isFileFormNameString = Object.prototype.toString.call(fileFormName) === '[object String]';
for (var i = 0; i < config.file.length; i++) {
formData.append(isFileFormNameString ? fileFormName + i : fileFormName[i], config.file[i], config.file[i].name);
}
} else {
formData.append(fileFormName, config.file, config.file.name);
}
} else {
formData.append(fileFormName, config.file, config.file.name);
}

config.data = formData;

return sendHttp(config);
};

this.http = function(config) {
return sendHttp(config);
}
Expand Down
7 changes: 6 additions & 1 deletion demo/war/js/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var MyCtrl = [ '$scope', '$http', '$timeout', '$upload', function($scope, $http
$scope.upload[index] = $upload.upload({
url : 'upload',
method: $scope.httpMethod,
headers: {'myHeaderKey': 'myHeaderVal'},
headers: {'my-header': 'my-header-value'},
data : {
myModel : $scope.myModel
},
Expand All @@ -69,12 +69,17 @@ var MyCtrl = [ '$scope', '$http', '$timeout', '$upload', function($scope, $http
fd.append(key, val);
}
}, */
/* transformRequest: [function(val, h) {
console.log(val, h('my-header')); return val + 'aaaaa';
}], */
file: $scope.selectedFiles[index],
fileFormDataName: 'myFile'
}).then(function(response) {
$scope.uploadResult.push(response.data);
}, null, function(evt) {
$scope.progress[index] = parseInt(100.0 * evt.loaded / evt.total);
}).xhr(function(xhr){
xhr.upload.addEventListener('abort', function(){console.log('aborted complete')}, false);
});
} else {
var fileReader = new FileReader();
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-file-upload-html5-shim.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* AngularJS file upload shim for angular XHR HTML5 browsers
* @author Danial <[email protected]>
* @version 1.2.9
* @version 1.2.10
*/
if (window.XMLHttpRequest) {
if (window.FormData) {
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-file-upload-html5-shim.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions dist/angular-file-upload-shim.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* AngularJS file upload shim for HTML5 FormData
* @author Danial <[email protected]>
* @version 1.2.9
* @version 1.2.10
*/
(function() {

Expand Down Expand Up @@ -45,13 +45,9 @@ if (window.XMLHttpRequest) {
xhr.__requestHeaders = [];
xhr.open = (function(orig) {
if (!xhr.upload) xhr.upload = {};
xhr.__listeners = [];
xhr.upload.addEventListener = function(t, fn, b) {
if (t === 'progress') {
xhr.__progress = fn;
}
if (t === 'load') {
xhr.__load = fn;
}
xhr.__listeners[t] = fn;
};
return function(m, url, b) {
orig.apply(xhr, [m, url, b]);
Expand Down Expand Up @@ -93,7 +89,12 @@ if (window.XMLHttpRequest) {
var config = {
url: xhr.__url,
complete: function(err, fileApiXHR) {
if (!err) xhr.__load({type: 'load', loaded: xhr.__total, total: xhr.__total, target: xhr, lengthComputable: true});
if (!err && xhr.__listeners['load'])
xhr.__listeners['load']({type: 'load', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (!err && xhr.__listeners['loadend'])
xhr.__listeners['loadend']({type: 'loadend', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (err === 'abort' && xhr.__listeners['abort'])
xhr.__listeners['abort']({type: 'abort', loaded: xhr.__loaded, total: xhr.__total, target: xhr, lengthComputable: true});
if (fileApiXHR.status !== undefined) Object.defineProperty(xhr, 'status', {get: function() {return fileApiXHR.status}});
if (fileApiXHR.statusText !== undefined) Object.defineProperty(xhr, 'statusText', {get: function() {return fileApiXHR.statusText}});
Object.defineProperty(xhr, 'readyState', {get: function() {return 4}});
Expand All @@ -104,8 +105,9 @@ if (window.XMLHttpRequest) {
},
progress: function(e) {
e.target = xhr;
xhr.__progress(e);
xhr.__listeners['progress'] && xhr.__listeners['progress'](e);
xhr.__total = e.total;
xhr.__loaded = e.loaded;
},
headers: xhr.__requestHeaders
}
Expand Down
Loading

0 comments on commit 5a32e27

Please sign in to comment.