Skip to content

Commit

Permalink
Fixed #1294
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial Farid authored and Danial Farid committed Jan 22, 2016
1 parent 8e673e3 commit d6f1cba
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 85 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ Upload.json(obj)
var blob = upload.dataUrltoBlob(dataurl, name);
/* returns true if there is an upload in progress. Can be used to prompt user before closing browser tab */
Upload.isUploadInProgress() boolean
/* downloads and converts a given url to Blob object which could be added to files model */
Upload.urlToBlob(url).then(function(blob) {...});
/* returns boolean to check if the object is file and could be used as file in Upload.upload()/http() */
Upload.isFile(obj);
```
**ng-model**
The model value will be a single file instead of an array if all of the followings are true:
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/FileAPI.min.js

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

41 changes: 27 additions & 14 deletions demo/src/main/webapp/js/ng-file-upload-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* progress, resize, thumbnail, preview, validation and CORS
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <[email protected]>
* @version 11.2.1
* @version 11.2.2
*/

(function () {
Expand Down Expand Up @@ -424,7 +424,7 @@ if (!window.FileReader) {
* AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort,
* progress, resize, thumbnail, preview, validation and CORS
* @author Danial <[email protected]>
* @version 11.2.1
* @version 11.2.2
*/

if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
Expand All @@ -445,7 +445,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {

var ngFileUpload = angular.module('ngFileUpload', []);

ngFileUpload.version = '11.2.1';
ngFileUpload.version = '11.2.2';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
var upload = this;
Expand Down Expand Up @@ -645,11 +645,11 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return clone;
}

this.upload = function (config, internal) {
function isFile(file) {
return file != null && (file instanceof window.Blob || (file.flashId && file.name && file.size));
}
this.isFile = function (file) {
return file != null && (file instanceof window.Blob || (file.flashId && file.name && file.size));
};

this.upload = function (config, internal) {
function toResumeFile(file, formData) {
if (file._ngfBlob) return file;
config._file = config._file || file;
Expand Down Expand Up @@ -679,7 +679,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}
if (angular.isString(val)) {
formData.append(key, val);
} else if (isFile(val)) {
} else if (upload.isFile(val)) {
var file = toResumeFile(val, formData);
var split = key.split(',');
if (split[1]) {
Expand Down Expand Up @@ -787,6 +787,21 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return str;
};

this.urlToBlob = function(url) {
var defer = $q.defer();
$http({url: url, method: 'get', responseType: 'arraybuffer'}).then(function (resp) {
var arrayBufferView = new Uint8Array(resp.data);
var type = resp.headers('content-type') || 'image/WebP';
var blob = new window.Blob([arrayBufferView], {type: type});
defer.resolve(blob);
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
}, function (e) {
defer.reject(e);
});
return defer.promise;
};

this.setDefaults = function (defaults) {
this.defaults = defaults || {};
};
Expand Down Expand Up @@ -1580,6 +1595,9 @@ ngFileUpload.service('UploadValidate', ['UploadDataUrl', '$q', '$timeout', funct
if (ngModel) {
ngModel.$formatters.push(function (files) {
if (ngModel.$dirty) {
if (!angular.isArray(files)) {
files = [files];
}
upload.validate(files, ngModel, attr, scope).then(function () {
upload.applyModelValidation(ngModel, files);
});
Expand Down Expand Up @@ -2258,13 +2276,8 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
var promises = [], files = [];
if (urls.length) {
angular.forEach(urls, function (url) {
promises.push($http({url: url, method: 'get', responseType: 'arraybuffer'}).then(function (resp) {
var arrayBufferView = new Uint8Array(resp.data);
var type = resp.headers('content-type') || 'image/WebP';
var blob = new window.Blob([arrayBufferView], {type: type});
promises.push(upload.urlToBlob(url).then(function (blob) {
files.push(blob);
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
}));
});
var defer = $q.defer();
Expand Down
7 changes: 4 additions & 3 deletions demo/src/main/webapp/js/ng-file-upload-all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* progress, resize, thumbnail, preview, validation and CORS
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <[email protected]>
* @version 11.2.1
* @version 11.2.2
*/

(function () {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.min.js

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

39 changes: 26 additions & 13 deletions demo/src/main/webapp/js/ng-file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort,
* progress, resize, thumbnail, preview, validation and CORS
* @author Danial <[email protected]>
* @version 11.2.1
* @version 11.2.2
*/

if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
Expand All @@ -23,7 +23,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {

var ngFileUpload = angular.module('ngFileUpload', []);

ngFileUpload.version = '11.2.1';
ngFileUpload.version = '11.2.2';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
var upload = this;
Expand Down Expand Up @@ -223,11 +223,11 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return clone;
}

this.upload = function (config, internal) {
function isFile(file) {
return file != null && (file instanceof window.Blob || (file.flashId && file.name && file.size));
}
this.isFile = function (file) {
return file != null && (file instanceof window.Blob || (file.flashId && file.name && file.size));
};

this.upload = function (config, internal) {
function toResumeFile(file, formData) {
if (file._ngfBlob) return file;
config._file = config._file || file;
Expand Down Expand Up @@ -257,7 +257,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
}
if (angular.isString(val)) {
formData.append(key, val);
} else if (isFile(val)) {
} else if (upload.isFile(val)) {
var file = toResumeFile(val, formData);
var split = key.split(',');
if (split[1]) {
Expand Down Expand Up @@ -365,6 +365,21 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
return str;
};

this.urlToBlob = function(url) {
var defer = $q.defer();
$http({url: url, method: 'get', responseType: 'arraybuffer'}).then(function (resp) {
var arrayBufferView = new Uint8Array(resp.data);
var type = resp.headers('content-type') || 'image/WebP';
var blob = new window.Blob([arrayBufferView], {type: type});
defer.resolve(blob);
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
}, function (e) {
defer.reject(e);
});
return defer.promise;
};

this.setDefaults = function (defaults) {
this.defaults = defaults || {};
};
Expand Down Expand Up @@ -1158,6 +1173,9 @@ ngFileUpload.service('UploadValidate', ['UploadDataUrl', '$q', '$timeout', funct
if (ngModel) {
ngModel.$formatters.push(function (files) {
if (ngModel.$dirty) {
if (!angular.isArray(files)) {
files = [files];
}
upload.validate(files, ngModel, attr, scope).then(function () {
upload.applyModelValidation(ngModel, files);
});
Expand Down Expand Up @@ -1836,13 +1854,8 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
var promises = [], files = [];
if (urls.length) {
angular.forEach(urls, function (url) {
promises.push($http({url: url, method: 'get', responseType: 'arraybuffer'}).then(function (resp) {
var arrayBufferView = new Uint8Array(resp.data);
var type = resp.headers('content-type') || 'image/WebP';
var blob = new window.Blob([arrayBufferView], {type: type});
promises.push(upload.urlToBlob(url).then(function (blob) {
files.push(blob);
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
}));
});
var defer = $q.defer();
Expand Down
6 changes: 3 additions & 3 deletions demo/src/main/webapp/js/ng-file-upload.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/FileAPI.min.js

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

Loading

0 comments on commit d6f1cba

Please sign in to comment.