Skip to content

Commit

Permalink
Fixed #433, #12, #434
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial Farid authored and Danial Farid committed Dec 4, 2014
1 parent 6233f9f commit 121188b
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 52 deletions.
27 changes: 7 additions & 20 deletions demo/src/com/df/angularfileupload/FileUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public class FileUpload extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try {
if (req.getParameter("errorCode") != null) {
// res.getWriter().write(req.getParameter("errorMessage"));
// res.getWriter().flush();
res.sendError(Integer.parseInt(req.getParameter("errorCode")), req.getParameter("errorMessage"));
return;
}
StringBuilder sb = new StringBuilder("{\"result\": [");
int errorCode = 0;
String errorMsg = null;

if (req.getHeader("Content-Type") != null
&& req.getHeader("Content-Type").startsWith("multipart/form-data")) {
Expand All @@ -33,18 +37,6 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S

while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.getFieldName() != null && item.getFieldName().equals("errorCode")) {
String val = read(item.openStream());
try {
errorCode = Integer.parseInt(val);
} catch(NumberFormatException e) {}
continue;
}
if (item.getFieldName() != null && item.getFieldName().equals("errorMessage")) {
String val = read(item.openStream());
errorMsg = val;
continue;
}
sb.append("{");
sb.append("\"fieldName\":\"").append(item.getFieldName()).append("\",");
if (item.getName() != null) {
Expand Down Expand Up @@ -77,12 +69,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
}
sb.append("}}");

if (errorCode != 0) {
res.setStatus(errorCode);
res.getWriter().write(errorMsg);
} else {
res.getWriter().write(sb.toString());
}
res.getWriter().write(sb.toString());
} catch (Exception ex) {
throw new ServletException(ex);
}
Expand Down
19 changes: 14 additions & 5 deletions demo/war/js/angular-file-upload-all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* AngularJS file upload/drop directive with progress and abort
* @author Danial <danial.farid@gmail.com>
* @version 2.0.2
* @version 2.0.3
*/
(function() {

Expand All @@ -26,7 +26,7 @@ if (window.XMLHttpRequest && !window.XMLHttpRequest.__isFileAPIShim) {
}

var angularFileUpload = angular.module('angularFileUpload', []);
angularFileUpload.version = '2.0.2';
angularFileUpload.version = '2.0.3';
angularFileUpload.service('$upload', ['$http', '$q', '$timeout', function($http, $q, $timeout) {
function sendHttp(config) {
config.method = config.method || 'POST';
Expand Down Expand Up @@ -193,8 +193,17 @@ function handleFileSelect(scope, elem, attr, ngModel, $parse, $timeout) {
}
if (scope.resetOnClick() != false) {
elem.bind('click', function(evt) {
this.value = null;
updateModel([], attr, ngModel, scope, evt);
if (elem[0].value) {
updateModel([], attr, ngModel, scope, evt);
}
elem[0].value = null;
});
}
if (ngModel) {
scope.$parent.$watch(attr['ngModel'], function(val) {
if (val == null) {
elem[0].value = null;
}
});
}
if (attr['ngFileSelect'] != '') {
Expand Down Expand Up @@ -465,7 +474,7 @@ function globStringToRegex(str) {
* AngularJS file upload/drop directive with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 2.0.2
* @version 2.0.3
*/

(function() {
Expand Down
13 changes: 11 additions & 2 deletions demo/war/js/angular-file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,17 @@ function handleFileSelect(scope, elem, attr, ngModel, $parse, $timeout) {
}
if (scope.resetOnClick() != false) {
elem.bind('click', function(evt) {
this.value = null;
updateModel([], attr, ngModel, scope, evt);
if (elem[0].value) {
updateModel([], attr, ngModel, scope, evt);
}
elem[0].value = null;
});
}
if (ngModel) {
scope.$parent.$watch(attr['ngModel'], function(val) {
if (val == null) {
elem[0].value = null;
}
});
}
if (attr['ngFileSelect'] != '') {
Expand Down
2 changes: 1 addition & 1 deletion demo/war/js/upload-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var fileReader = new FileReader();
fileReader.onload = function(e) {
$timeout(function() {
file.upload = $upload.http({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
url: 'https://angular-file-upload-cors-srv.appspot.com/upload' + $scope.getReqParams(),
method: 'POST',
headers : {
'Content-Type': file.type
Expand Down
7 changes: 1 addition & 6 deletions demo/war/js/upload-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ var data = $scope.formUpload ? {
username: $scope.username
} : {};

if ($scope.generateErrorOnServer) {
data.errorCode = $scope.serverErrorCode;
data.errorMessage = $scope.serverErrorMsg;
}

file.upload = $upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
url: 'https://angular-file-upload-cors-srv.appspot.com/upload' + $scope.getReqParams(),
method: 'POST',
headers: {
'my-header' : 'my-header-value'
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 @@ -30,7 +30,7 @@ app.controller('MyCtrl', [ '$scope', '$http', '$timeout', '$compile', '$upload',
}
storeS3UploadConfigInLocalStore();
};

$scope.uploadPic = function(files) {
$scope.formUpload = true;
if (files != null) {
Expand Down Expand Up @@ -144,4 +144,9 @@ app.controller('MyCtrl', [ '$scope', '$http', '$timeout', '$compile', '$upload',
$scope.confirm = function() {
return confirm('Are you sure? Your local changes will be lost.');
}

$scope.getReqParams = function() {
return $scope.generateErrorOnServer ? "?errorCode=" + $scope.serverErrorCode +
"&errorMessage=" + $scope.serverErrorMsg : "";
}
} ]);
19 changes: 14 additions & 5 deletions dist/angular-file-upload-all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* AngularJS file upload/drop directive with progress and abort
* @author Danial <danial.farid@gmail.com>
* @version 2.0.2
* @version 2.0.3
*/
(function() {

Expand All @@ -26,7 +26,7 @@ if (window.XMLHttpRequest && !window.XMLHttpRequest.__isFileAPIShim) {
}

var angularFileUpload = angular.module('angularFileUpload', []);
angularFileUpload.version = '2.0.2';
angularFileUpload.version = '2.0.3';
angularFileUpload.service('$upload', ['$http', '$q', '$timeout', function($http, $q, $timeout) {
function sendHttp(config) {
config.method = config.method || 'POST';
Expand Down Expand Up @@ -193,8 +193,17 @@ function handleFileSelect(scope, elem, attr, ngModel, $parse, $timeout) {
}
if (scope.resetOnClick() != false) {
elem.bind('click', function(evt) {
this.value = null;
updateModel([], attr, ngModel, scope, evt);
if (elem[0].value) {
updateModel([], attr, ngModel, scope, evt);
}
elem[0].value = null;
});
}
if (ngModel) {
scope.$parent.$watch(attr['ngModel'], function(val) {
if (val == null) {
elem[0].value = null;
}
});
}
if (attr['ngFileSelect'] != '') {
Expand Down Expand Up @@ -465,7 +474,7 @@ function globStringToRegex(str) {
* AngularJS file upload/drop directive with progress and abort
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <danial.farid@gmail.com>
* @version 2.0.2
* @version 2.0.3
*/

(function() {
Expand Down
Loading

0 comments on commit 121188b

Please sign in to comment.