18
18
var app = express ();
19
19
app .configure (function () {
20
20
...
21
- app .use (' /upload' , upload ({
21
+ app .use (' /upload' , upload . fileHandler ({
22
22
uploadDir: __dirname + ' /public/uploads' ,
23
- uploadUrl: ' /uploads/ '
23
+ uploadUrl: ' /uploads'
24
24
}));
25
25
app .use (express .bodyParser ());
26
26
...
@@ -34,17 +34,99 @@ On the frontend:
34
34
<script >$ (' #fileupload' ).fileupload ({ dataType: ' json' }) </script >
35
35
```
36
36
37
+ More sophisticated example - Events
38
+
39
+ ``` javascript
40
+ app .use (' /upload' , upload .fileHandler ({
41
+ uploadDir: __dirname + ' /public/uploads' ,
42
+ uploadUrl: ' /uploads'
43
+ }));
44
+
45
+ // events
46
+ upload .on (' begin' , function (fileInfo ) { ... });
47
+ upload .on (' abort' , function (fileInfo ) { ... });
48
+ upload .on (' end' , function (fileInfo ) {
49
+ // fileInfo structure is the same as returned to browser
50
+ // {
51
+ // name: '3 (3).jpg',
52
+ // originalName: '3.jpg',
53
+ // size: 79262,
54
+ // type: 'image/jpeg',
55
+ // delete_type: 'DELETE',
56
+ // delete_url: 'http://yourhost/upload/3%20(3).jpg',
57
+ // url: 'http://yourhost/uploads/3%20(3).jpg',
58
+ // thumbnail_url: 'http://youhost/uploads/thumbnail/3%20(3).jpg'
59
+ // }
60
+ });
61
+ upload .on (' error' , function (e ) {
62
+ console .log (e .message );
63
+ });
64
+ ```
65
+
66
+ Dynamic upload directory and url, isolating user files:
67
+
68
+ ``` javascript
69
+ app .use (' /upload' , function (req , res , next ) {
70
+ upload .fileHandler ({
71
+ uploadDir : function () {
72
+ return __dirname + ' /public/uploads/' + req .sessionID
73
+ },
74
+ uploadUrl : function () {
75
+ return ' /uploads/' + req .sessionID
76
+ },
77
+ imageVersions: {
78
+ thumbnail: {
79
+ width: 80 ,
80
+ height: 80
81
+ }
82
+ }
83
+ })(req, res, next);
84
+ });
85
+ ```
86
+
87
+ Getting uploaded files mapped to their fs locations:
88
+
89
+ ``` javascript
90
+ app .use (' /list' , function (req , res , next ) {
91
+ upload .getFiles ({
92
+ uploadDir : function () {
93
+ return __dirname + ' /public/uploads/' + req .sessionID
94
+ },
95
+ uploadUrl : function () {
96
+ return ' /uploads/' + req .sessionID
97
+ },
98
+ imageVersions: {
99
+ thumbnail: {
100
+ width: 80 ,
101
+ height: 80
102
+ }
103
+ }
104
+ }, function (files ) {
105
+ // {
106
+ // "00001.MTS": {
107
+ // "path": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/00001.MTS"
108
+ // },
109
+ // "DSC00030.JPG": {
110
+ // "path": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/DSC00030.JPG",
111
+ // "thumbnail": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/thumbnail/DSC00030.JPG"
112
+ // }
113
+ // }
114
+ res .json (files);
115
+ });
116
+ });
117
+ ```
118
+
37
119
Other options and their default values:
120
+
38
121
``` javascript
39
122
{
40
123
tmpDir: ' /tmp' ,
124
+ uploadDir: __dirname + ' /public/uploads' ,
125
+ uploadUrl: ' /uploads' ,
41
126
maxPostSize: 11000000000 , // 11 GB
42
127
minFileSize: 1 ,
43
128
maxFileSize: 10000000000 , // 10 GB
44
129
acceptFileTypes: / . + / i ,
45
- // Files not matched by this regular expression force a download dialog,
46
- // to prevent executing any scripts in the context of the service domain:
47
- safeFileTypes: / \. (gif| jpe? g| png)$ / i ,
48
130
imageTypes: / \. (gif| jpe? g| png)$ / i ,
49
131
imageVersions: {
50
132
thumbnail: {
0 commit comments