Skip to content

Commit e3377a6

Browse files
author
Andrew Valums
committed
1 parent f4bf836 commit e3377a6

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

client/fileuploader.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ qq.UploadHandlerAbstract = function(o){
867867
qq.extend(this._options, o);
868868

869869
this._queue = [];
870+
// params for files in queue
871+
this._params = [];
870872
};
871873
qq.UploadHandlerAbstract.prototype = {
872874
/**
@@ -878,11 +880,16 @@ qq.UploadHandlerAbstract.prototype = {
878880
* Sends the file identified by id and additional query params to the server
879881
*/
880882
upload: function(id, params){
881-
var len = this._queue.push(id);
882-
// too many active uploads, wait...
883-
if (len > this._options.maxConnections) return;
884-
885-
this._upload(id, params);
883+
var len = this._queue.push(id);
884+
885+
var copy = {};
886+
qq.extend(copy, params);
887+
this._params[id] = copy;
888+
889+
// if too many active uploads, wait...
890+
if (len <= this._options.maxConnections){
891+
this._upload(id, this._params[id]);
892+
}
886893
},
887894
/**
888895
* Cancels file upload by id
@@ -933,7 +940,8 @@ qq.UploadHandlerAbstract.prototype = {
933940
var max = this._options.maxConnections;
934941

935942
if (this._queue.length >= max){
936-
this._upload(this._queue[max-1]);
943+
var nextId = this._queue[max-1];
944+
this._upload(nextId, this._params[nextId]);
937945
}
938946
}
939947
};
@@ -1128,6 +1136,10 @@ qq.extend(qq.UploadHandlerXhr.prototype, {
11281136
* Returns id to use with upload, cancel
11291137
**/
11301138
add: function(file){
1139+
if (!(file instanceof File)){
1140+
throw new Error('Passed obj in not a File (in qq.UploadHandlerXhr)');
1141+
}
1142+
11311143
return this._files.push(file) - 1;
11321144
},
11331145
getName: function(id){

tests/action-handler-queue-test.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
sleep(4);
4+
5+
$fileName;
6+
7+
if (isset($_GET['qqfile'])){
8+
$fileName = $_GET['qqfile'];
9+
10+
// xhr request
11+
$headers = apache_request_headers();
12+
if ((int)$headers['Content-Length'] == 0){
13+
die ('{error: "content length is zero"}');
14+
}
15+
} elseif (isset($_FILES['qqfile'])){
16+
$fileName = basename($_FILES['qqfile']['name']);
17+
18+
// form request
19+
if ($_FILES['qqfile']['size'] == 0){
20+
die ('{error: "file size is zero"}');
21+
}
22+
} else {
23+
die ('{error: "file not passed"}');
24+
}
25+
26+
if (count($_GET)){
27+
$_GET['success'] = true;
28+
echo json_encode(array_merge($_GET));
29+
} else {
30+
die ('{error: "query params not passed"}');
31+
}

tests/test-handler-queue.htm

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
5+
6+
<link href="qunit/qunit/qunit.css" rel="stylesheet" type="text/css" media="screen" />
7+
<script src="qunit/qunit/qunit.js" type="text/javascript"></script>
8+
9+
<script src="../client/fileuploader.js" type="text/javascript" ></script>
10+
<script>
11+
jQuery(function(){
12+
13+
function getHandler(){
14+
if(qq.UploadHandlerXhr.isSupported()){
15+
return qq.UploadHandlerXhr;
16+
} else {
17+
return qq.UploadHandlerForm;
18+
}
19+
}
20+
21+
asyncTest("upload", function() {
22+
expect(2);
23+
24+
var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
25+
var savedId;
26+
27+
var uploadHandler = new (getHandler())({
28+
action: 'action-handler-queue-test.php',
29+
maxConnections: 1,
30+
onComplete: function(id, fileName, response){
31+
if (!response.success){
32+
ok(false, 'server did not receive file')
33+
return;
34+
}
35+
36+
delete response.success;
37+
delete response.qqfile;
38+
39+
same(response, data, 'server received file and data');
40+
}
41+
});
42+
43+
44+
$('#testinput1, #testinput2').change(upload);
45+
46+
function upload(){
47+
setTimeout(start, 9000);
48+
49+
var file = this;
50+
if (uploadHandler instanceof qq.UploadHandlerXhr){
51+
file = this.files[0];
52+
}
53+
var id = uploadHandler.add(file);
54+
uploadHandler.upload(id, data);
55+
}
56+
57+
58+
});
59+
});
60+
</script>
61+
</head>
62+
<body>
63+
<h1 id="qunit-header">File uploader tests</h1>
64+
<h2 id="qunit-banner"></h2>
65+
<h2 id="qunit-userAgent"></h2>
66+
<ol id="qunit-tests"></ol>
67+
68+
69+
<p>
70+
Please select a file for each input below,
71+
should be less than 4 sec, between selection.
72+
</p>
73+
74+
75+
<input id="testinput1" type="file">
76+
<input id="testinput2" type="file">
77+
78+
</body>
79+
</html>
80+
81+

0 commit comments

Comments
 (0)