Skip to content

Commit

Permalink
fixed #1183, fixed #1130
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial Farid authored and Danial Farid committed Dec 7, 2015
1 parent e472a1f commit 4cdec8f
Show file tree
Hide file tree
Showing 21 changed files with 320 additions and 136 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Upload on form submit or button click
<form ng-app="fileUpload" ng-controller="MyCtrl" name="form">
Single Image with validations
<div class="button" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'"
accept="image/*" ngf-max-size="20MB" ngf-min-height="100"
ngf-accept="'image/*'" ngf-max-size="20MB" ngf-min-height="100"

This comment has been minimized.

Copy link
@mxshrestha

mxshrestha May 1, 2016

your examples uses just "accept" instead of ngf-accept. Also, how is this different from ngf-pattern?

This comment has been minimized.

Copy link
@danialfarid

danialfarid May 6, 2016

Owner

ngf-pattern is programmatically validation. Accept is browser specific.

ngf-resize="{width: 100, height: 100}">Select</div>
Multiple files
<div class="button" ngf-select ng-model="files" ngf-multiple="true">Select</div>
Expand Down Expand Up @@ -178,7 +178,7 @@ At least one of the `ngf-select` or `ngf-drop` are mandatory for the plugin to l
// to decide wethere to fix that file or not.

*ngf-capture="'camera'" or "'other'" // allows mobile devices to capture using camera
*accept="image/*" // standard HTML accept attribute for the browser specific popup window filtering
*ngf-accept="'image/*'" // standard HTML accept attr, browser specific select popup window

+ngf-allow-dir="boolean" // default true, allow dropping files only for Chrome webkit browser
+ngf-drag-over-class="{pattern: 'image/*', accept:'acceptClass', reject:'rejectClass', delay:100}"
Expand Down
3 changes: 1 addition & 2 deletions demo/src/main/webapp/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ fieldset label {
background: #F8F8F8;
border: 5px dashed #DDD;
width: 170px;
height: 170px;
text-align: center;
padding: 100px 10px 0 10px;
padding: 50px 10px;
margin-left: 10px;
}

Expand Down
20 changes: 14 additions & 6 deletions demo/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h3>
<legend>Upload on form submit</legend>
Username: <input type="text" name="userName" ng-model="username" size="39" required>
<i ng-show="myForm.userName.$error.required">*required</i><br>
Profile Picture: <input type="file" ngf-select ng-model="picFile" name="file" accept="image/*" required>
Profile Picture: <input type="file" ngf-select ng-model="picFile" name="file" ngf-accept="'image/*'" required>
<i ng-show="myForm.file.$error.required">*required</i>
<br/>

Expand All @@ -89,15 +89,25 @@ <h3>
<div class="up-buttons">
<div ngf-select ngf-drop ng-model="files" ngf-model-invalid="invalidFiles"
ng-model-options="modelOptionsObj"
ngf-multiple="multiple" ngf-pattern="pattern" accept="{{acceptSelect}}"
ngf-multiple="multiple" ngf-pattern="pattern" ngf-accept="acceptSelect"
ng-disabled="disabled" ngf-capture="capture"
ngf-drag-over-class="dragOverClassObj"
ngf-validate="validateObj"
ngf-keep="keepDistinct ? 'distinct' : keep"
ngf-allow-dir="allowDir" class="drop-box" ngf-drop-available="dropAvailable">Select File,
<span ng-show="dropAvailable">Drop File or Paste/Drop Image</span>
ngf-allow-dir="allowDir" class="drop-box" ngf-drop-available="dropAvailable">Select File
<span ng-show="dropAvailable"> or Drop File</span>
</div>
<br/>
<div ngf-drop ng-model="files" ngf-model-invalid="invalidFiles"
ng-model-options="modelOptionsObj"
ngf-multiple="multiple" ngf-pattern="'image/*'"
ng-disabled="disabled"
ngf-drag-over-class="dragOverClassObj"
ngf-validate="validateObj"
ngf-keep="keepDistinct ? 'distinct' : keep"
ngf-enable-firefox-paste="true"
ngf-allow-dir="allowDir" class="drop-box" ng-show="dropAvailable">
<span>Paste or Drop Image from browser</span></div>
</div>
<div class="custom">
<label>accept (for select browser dependent): <input type="text" ng-model="acceptSelect"></label><br/>
Expand Down Expand Up @@ -233,7 +243,5 @@ <h3>
href="https://angular-file-upload.appspot.com/donate.html">donate</a>
<a style="position:fixed;bottom:45px;right:10px;font-size:smaller;"
href="https://github.com/danialfarid/angular-file-upload/issues/new">Feedback/Issues</a>

<div style="position:fixed;bottom:10px;right:10px;font-size:smaller;color:#777">Last update: 2015-09-19</div>
</body>
</html>
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.

79 changes: 57 additions & 22 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 10.1.8
* @version 10.1.9
*/

(function () {
Expand Down Expand Up @@ -421,7 +421,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 10.1.8
* @version 10.1.9
*/

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

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

ngFileUpload.version = '10.1.8';
ngFileUpload.version = '10.1.9';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
var upload = this;
Expand Down Expand Up @@ -1992,13 +1992,13 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
}]);

(function () {
ngFileUpload.directive('ngfDrop', ['$parse', '$timeout', '$location', 'Upload', '$http',
function ($parse, $timeout, $location, Upload, $http) {
ngFileUpload.directive('ngfDrop', ['$parse', '$timeout', '$location', 'Upload', '$http', '$q',
function ($parse, $timeout, $location, Upload, $http, $q) {
return {
restrict: 'AEC',
require: '?ngModel',
link: function (scope, elem, attr, ngModel) {
linkDrop(scope, elem, attr, ngModel, $parse, $timeout, $location, Upload, $http);
linkDrop(scope, elem, attr, ngModel, $parse, $timeout, $location, Upload, $http, $q);
}
};
}]);
Expand All @@ -2023,7 +2023,7 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
};
}]);

function linkDrop(scope, elem, attr, ngModel, $parse, $timeout, $location, upload, $http) {
function linkDrop(scope, elem, attr, ngModel, $parse, $timeout, $location, upload, $http, $q) {
var available = dropAvailable();

var attrGetter = function (name, scope, params) {
Expand Down Expand Up @@ -2102,22 +2102,10 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
var html;
try {
html = (evt.dataTransfer && evt.dataTransfer.getData && evt.dataTransfer.getData('text/html'));
} catch (e) {/* Fix IE11 that throw error calling getData */}
} catch (e) {/* Fix IE11 that throw error calling getData */
}
if (upload.shouldUpdateOn('dropUrl', attr, scope) && html) {
var url;
html.replace(/<img .*src *=\"([^\"]*)\"/, function (m, src) {
url = src;
});
if (url) {
$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});
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
upload.updateModel(ngModel, attr, scope, attrGetter('ngfChange') || attrGetter('ngfDrop'), [blob], evt);
});
}
extractUrlAndUpdateModel(html, evt);
} else {
extractFiles(evt, function (files) {
upload.updateModel(ngModel, attr, scope, attrGetter('ngfChange') || attrGetter('ngfDrop'), files, evt);
Expand All @@ -2126,6 +2114,9 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
}
}, false);
elem[0].addEventListener('paste', function (evt) {
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
evt.preventDefault();
}
if (isDisabled() || !upload.shouldUpdateOn('paste', attr, scope)) return;
var files = [];
var clipboard = evt.clipboardData || evt.originalEvent.clipboardData;
Expand All @@ -2135,10 +2126,54 @@ ngFileUpload.service('UploadResize', ['UploadValidate', '$q', function (UploadVa
files.push(clipboard.items[k].getAsFile());
}
}
}
if (files.length) {
upload.updateModel(ngModel, attr, scope, attrGetter('ngfChange') || attrGetter('ngfDrop'), files, evt);
} else {
var html;
try {
html = (clipboard && clipboard.getData && clipboard.getData('text/html'));
} catch (e) {/* Fix IE11 that throw error calling getData */
}
if (upload.shouldUpdateOn('pasteUrl', attr, scope) && html) {
extractUrlAndUpdateModel(html, evt);
}
}
}, false);

if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1 &&
attrGetter('ngfEnableFirefoxPaste', scope)) {
elem.attr('contenteditable', true);
elem.on('keypress', function (e) {
if (!e.metaKey && !e.ctrlKey) {
e.preventDefault();
}
});
}

function extractUrlAndUpdateModel(html, evt) {
var urls = [];
html.replace(/<(img src|img [^>]* src) *=\"([^\"]*)\"/gi, function (m, n, src) {
urls.push(src);
});
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});
files.push(blob);
//var split = type.split('[/;]');
//blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg');
}));
});
$q.all(promises).then(function () {
upload.updateModel(ngModel, attr, scope, attrGetter('ngfChange') || attrGetter('ngfDrop'), files, evt);
});
}
}

function calculateDragOverClass(scope, attr, evt, callback) {
var obj = attrGetter('ngfDragOverClass', scope, {$event: evt}), dClass = 'dragover';
if (angular.isString(obj)) {
Expand Down
7 changes: 3 additions & 4 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 10.1.8
* @version 10.1.9
*/

(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.

Loading

0 comments on commit 4cdec8f

Please sign in to comment.