Skip to content

Commit fa1f9bc

Browse files
committed
1 parent 62187c1 commit fa1f9bc

File tree

8 files changed

+53
-51
lines changed

8 files changed

+53
-51
lines changed

demo/src/com/df/angularfileupload/FileUpload.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public class FileUpload extends HttpServlet {
2222
@Override
2323
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
2424
try {
25+
StringBuilder sb = new StringBuilder("{\"result\": [");
26+
2527
if (req.getHeader("Content-Type") != null
2628
&& req.getHeader("Content-Type").startsWith("multipart/form-data")) {
2729
ServletFileUpload upload = new ServletFileUpload();
2830

2931
FileItemIterator iterator = upload.getItemIterator(req);
3032

31-
StringBuilder sb = new StringBuilder("{\"result\": [");
32-
3333
while (iterator.hasNext()) {
3434
sb.append("{");
3535
FileItemStream item = iterator.next();
@@ -47,37 +47,38 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
4747
sb.append(",");
4848
}
4949
}
50-
sb.append("]");
51-
sb.append(", \"requestHeaders\": {");
52-
@SuppressWarnings("unchecked")
53-
Enumeration<String> headerNames = req.getHeaderNames();
54-
while (headerNames.hasMoreElements()) {
55-
String header = headerNames.nextElement();
56-
sb.append("\"").append(header).append("\":\"").append(req.getHeader(header)).append("\"");
57-
if (headerNames.hasMoreElements()) {
58-
sb.append(",");
59-
}
60-
}
61-
sb.append("}}");
62-
63-
res.setContentType("application/json");
64-
PrintWriter printWriter = new PrintWriter(res.getOutputStream());
65-
try {
66-
printWriter.print(sb.toString());
67-
printWriter.flush();
68-
} finally {
69-
printWriter.close();
70-
}
7150
} else {
7251
res.setContentType("application/json");
7352
PrintWriter printWriter = new PrintWriter(res.getOutputStream());
7453
try {
75-
printWriter.print("{\"result\":[{\"size\":" + size(req.getInputStream()) + "}]}");
54+
sb.append("{\"size\":\"" + size(req.getInputStream()) + "\"}");
7655
printWriter.flush();
7756
} finally {
7857
printWriter.close();
7958
}
8059
}
60+
61+
sb.append("]");
62+
sb.append(", \"requestHeaders\": {");
63+
@SuppressWarnings("unchecked")
64+
Enumeration<String> headerNames = req.getHeaderNames();
65+
while (headerNames.hasMoreElements()) {
66+
String header = headerNames.nextElement();
67+
sb.append("\"").append(header).append("\":\"").append(req.getHeader(header)).append("\"");
68+
if (headerNames.hasMoreElements()) {
69+
sb.append(",");
70+
}
71+
}
72+
sb.append("}}");
73+
74+
res.setContentType("application/json");
75+
PrintWriter printWriter = new PrintWriter(res.getOutputStream());
76+
try {
77+
printWriter.print(sb.toString());
78+
printWriter.flush();
79+
} finally {
80+
printWriter.close();
81+
}
8182
} catch (Exception ex) {
8283
throw new ServletException(ex);
8384
}
@@ -90,6 +91,9 @@ protected int size(InputStream stream) {
9091
int size;
9192
while ((size = stream.read(buffer)) != -1) {
9293
length += size;
94+
// for (int i = 0; i < size; i++) {
95+
// System.out.print((char) buffer[i]);
96+
// }
9397
}
9498
} catch (IOException e) {
9599
throw new RuntimeException(e);

demo/war/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ <h3>
3333
<label><input type="radio" name="howToSend" ng-model="howToSend" value="2" ng-disabled="!dropSupported">File binary content with the file type as Content-Type header using $upload.http() service</label>
3434
<div class="sub">The second option could be used to upload files to <a href="https://github.com/danialfarid/angular-file-upload/issues/88">CouchDB</a>,
3535
<a href="https://github.com/danialfarid/angular-file-upload/issues/87">imgur</a>, etc... for HTML5 FileReader browsers.<br/>
36-
Option 2 cross browser FileReader and image preview will be added around Feb 2014 when FileAPI FileReader support is available.<br/>
3736
</div><br/>
38-
myModel: <input type="text" ng-model="myModel"> model object to be sent with the file.<br/>
37+
myModel: <input type="text" ng-model="myModel"> model object to be sent with the file.<br/>
3938
choose a single file: <input type="file" ng-file-select="onFileSelect($files)">
4039
<br/>
4140
or multiple files: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="file" ng-file-select="onFileSelect($files)" multiple>

demo/war/js/FileAPI.flash.swf.new

-36.1 KB
Binary file not shown.

demo/war/js/FileAPI.min.js.new

Lines changed: 0 additions & 3 deletions
This file was deleted.

demo/war/js/angular-file-upload-shim.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ if (!window.FormData) {
139139
var wrapFileApi = function(elem) {
140140
if (!elem.__isWrapped && (elem.getAttribute('ng-file-select') != null || elem.getAttribute('data-ng-file-select') != null)) {
141141
var wrap = document.createElement('div');
142-
wrap.innerHTML = '<div class="js-fileapi-wrapper" /*style="position:relative; overflow:hidden"*/></div>';
142+
wrap.innerHTML = '<div class="js-fileapi-wrapper" style="position:relative; overflow:hidden"></div>';
143143
wrap = wrap.firstChild;
144144
var parent = elem.parentNode;
145145
parent.insertBefore(wrap, elem);
@@ -205,12 +205,15 @@ if (!window.FormData) {
205205

206206
(function () {
207207
//load FileAPI
208-
if (!window.FileAPI || !FileAPI.upload) {
209-
var base = '', jsUrl = null, script = document.createElement('script'), allScripts = document.getElementsByTagName('script'), i, index, src;
210-
if (window.FileAPI && window.FileAPI.jsPath) {
211-
base = window.FileAPI.jsPath;
212-
} else if (window.FileAPI && window.FileAPI.jsUrl){
213-
jsUrl = window.FileAPI.jsUrl
208+
if (!window.FileAPI) {
209+
window.FileAPI = {};
210+
}
211+
if (!FileAPI.upload) {
212+
var jsUrl, basePath, script = document.createElement('script'), allScripts = document.getElementsByTagName('script'), i, index, src;
213+
if (window.FileAPI.jsUrl) {
214+
jsUrl = window.FileAPI.jsUrl;
215+
} else if (window.FileAPI.jsPath) {
216+
basePath = window.FileAPI.jsPath;
214217
} else {
215218
for (i = 0; i < allScripts.length; i++) {
216219
src = allScripts[i].src;
@@ -219,25 +222,21 @@ if (!window.FormData) {
219222
index = src.indexOf('angular-file-upload-shim.min.js');
220223
}
221224
if (index > -1) {
222-
base = src.substring(0, index);
225+
basePath = src.substring(0, index);
223226
break;
224227
}
225228
}
226229
}
227230

228-
if (!window.FileAPI || FileAPI.staticPath == null) {
229-
FileAPI = {
230-
staticPath: base
231-
}
232-
}
233-
234-
script.setAttribute('src', jsUrl || base + "FileAPI.min.js");
231+
if (FileAPI.staticPath == null) FileAPI.staticPath = basePath;
232+
FileAPI.debug = true;
233+
script.setAttribute('src', jsUrl || basePath + "FileAPI.min.js");
235234
document.getElementsByTagName('head')[0].appendChild(script);
236235
}
237236
})();
238237
}
239238

240-
/*
239+
241240
if (!window.FileReader) {
242241
window.FileReader = function() {
243242
var _this = this, loadStarted = false;
@@ -285,7 +284,7 @@ if (!window.FileReader) {
285284
}
286285
};
287286
this.readAsArrayBuffer = function(file) {
288-
FileAPI.readAsArrayBuffer(file, listener);
287+
FileAPI.readAsBinaryString(file, listener);
289288
}
290289
this.readAsBinaryString = function(file) {
291290
FileAPI.readAsBinaryString(file, listener);
@@ -298,5 +297,5 @@ if (!window.FileReader) {
298297
}
299298
}
300299
}
301-
*/
300+
302301
})();

demo/war/js/angular-file-upload.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ angularFileUpload.service('$upload', ['$http', '$rootScope', '$timeout', functio
5858
promise.then = (function(promise, origThen) {
5959
return function(s, e, p) {
6060
config.progress = p || config.progress;
61-
return origThen.apply(promise, [s, e, p]);
61+
var result = origThen.apply(promise, [s, e, p]);
62+
result.abort = promise.abort;
63+
result.progress = promise.progress;
64+
return result;
6265
};
6366
})(promise, promise.then);
6467

demo/war/js/upload.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ var MyCtrl = [ '$scope', '$http', '$timeout', '$upload', function($scope, $http
7878
});
7979
} else {
8080
var fileReader = new FileReader();
81-
fileReader.readAsArrayBuffer($scope.selectedFiles[index]);
8281
fileReader.onload = function(e) {
8382
$scope.upload[index] = $upload.http({
8483
url: 'upload',
8584
headers: {'Content-Type': $scope.selectedFiles[index].type},
8685
data: e.target.result
8786
}).then(function(response) {
88-
$scope.uploadResult.push(response.data.result);
87+
$scope.uploadResult.push(response.data);
8988
}, null, function(evt) {
9089
// Math.min is to fix IE which reports 200% sometimes
9190
$scope.progress[index] = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
9291
});
9392
}
93+
fileReader.readAsArrayBuffer($scope.selectedFiles[index]);
94+
9495
}
9596
}
9697
} ];

nuget/Package.nuspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<description>Light-weight HTML5 and cross-browser AngularJS directives for file upload, progress, abort, drag and drop</description>
1313
<tags>angularjs upload</tags>
1414
<dependencies>
15-
<dependency id="angularjs" version="1.0.7" />
1615
</dependencies>
1716
</metadata>
1817
</package>

0 commit comments

Comments
 (0)