@@ -16,15 +16,26 @@ Usage:
16
16
upload = require (' jquery-file-upload-middleware' );
17
17
18
18
var app = express ();
19
+
20
+ // configure upload middleware
21
+ upload .configure ({
22
+ uploadDir: __dirname + ' /public/uploads' ,
23
+ uploadUrl: ' /uploads' ,
24
+ imageVersions: {
25
+ thumbnail: {
26
+ width: 80 ,
27
+ height: 80
28
+ }
29
+ }
30
+ });
31
+
19
32
app .configure (function () {
20
33
...
21
- app .use (' /upload' , upload .fileHandler ({
22
- uploadDir: __dirname + ' /public/uploads' ,
23
- uploadUrl: ' /uploads'
24
- }));
34
+ app .use (' /upload' , upload .fileHandler ());
25
35
app .use (express .bodyParser ());
26
36
...
27
37
});
38
+
28
39
```
29
40
30
41
On the frontend:
@@ -34,13 +45,27 @@ On the frontend:
34
45
<script >$ (' #fileupload' ).fileupload ({ dataType: ' json' }) </script >
35
46
```
36
47
48
+ Overriding global configuration
49
+
50
+ ``` javascript
51
+
52
+ app .use (' /upload2' , upload .fileHandler ({
53
+ uploadDir: __dirname + ' /public/uploads2' ,
54
+ uploadUrl: ' /uploads2' ,
55
+ imageVersions: {
56
+ thumbnail: {
57
+ width: 100 ,
58
+ height: 100
59
+ }
60
+ }
61
+ }));
62
+
63
+ ```
64
+
37
65
More sophisticated example - Events
38
66
39
67
``` javascript
40
- app .use (' /upload' , upload .fileHandler ({
41
- uploadDir: __dirname + ' /public/uploads' ,
42
- uploadUrl: ' /uploads'
43
- }));
68
+ app .use (' /upload' , upload .fileHandler ());
44
69
45
70
// events
46
71
upload .on (' begin' , function (fileInfo ) { ... });
@@ -66,19 +91,23 @@ More sophisticated example - Events
66
91
Dynamic upload directory and url, isolating user files:
67
92
68
93
``` javascript
94
+ upload .configure ({
95
+ imageVersions: {
96
+ thumbnail: {
97
+ width: 80 ,
98
+ height: 80
99
+ }
100
+ }
101
+ });
102
+
69
103
app .use (' /upload' , function (req , res , next ) {
104
+ // imageVersions are taken from upload.configure()
70
105
upload .fileHandler ({
71
106
uploadDir : function () {
72
107
return __dirname + ' /public/uploads/' + req .sessionID
73
108
},
74
109
uploadUrl : function () {
75
110
return ' /uploads/' + req .sessionID
76
- },
77
- imageVersions: {
78
- thumbnail: {
79
- width: 80 ,
80
- height: 80
81
- }
82
111
}
83
112
})(req, res, next);
84
113
});
@@ -94,12 +123,6 @@ Getting uploaded files mapped to their fs locations:
94
123
},
95
124
uploadUrl : function () {
96
125
return ' /uploads/' + req .sessionID
97
- },
98
- imageVersions: {
99
- thumbnail: {
100
- width: 80 ,
101
- height: 80
102
- }
103
126
}
104
127
}, function (files ) {
105
128
// {
@@ -116,6 +139,24 @@ Getting uploaded files mapped to their fs locations:
116
139
});
117
140
```
118
141
142
+ Passing uploaded files down the request chain:
143
+
144
+ ``` javascript
145
+ app .use (' /api' , function (req , res , next ) {
146
+ upload .getFiles ({
147
+ uploadDir : function () {
148
+ return __dirname + ' /public/uploads/' + req .sessionID
149
+ },
150
+ uploadUrl : function () {
151
+ return ' /uploads/' + req .sessionID
152
+ }
153
+ }, function (files ) {
154
+ res .jquploadfiles = files;
155
+ next ();
156
+ });
157
+ });
158
+ ```
159
+
119
160
Other options and their default values:
120
161
121
162
``` javascript
@@ -144,7 +185,3 @@ Other options and their default values:
144
185
## License
145
186
Copyright (c) 2012 [ Aleksandr Guidrevitch] ( http://aguidrevitch.blogspot.com/ )
146
187
Released under the [ MIT license] ( http://www.opensource.org/licenses/MIT ) .
147
-
148
-
149
-
150
-
0 commit comments