|
1 |
| -jquery-file-upload-middleware |
| 1 | +jquery-file-upload-middleware-fix |
2 | 2 | =============================
|
3 | 3 |
|
4 | 4 | jQuery-File-Upload Express.js middleware. Based on the server code of [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload)
|
5 | 5 |
|
6 | 6 | Installation:
|
7 | 7 |
|
8 | 8 | ```
|
9 |
| - $ npm install jquery-file-upload-middleware |
| 9 | + $ npm install jquery-file-upload-middleware-fix |
10 | 10 | ```
|
11 | 11 |
|
12 |
| -Usage: |
13 |
| - |
14 |
| -```javascript |
15 |
| - var express = require("express"), |
16 |
| - upload = require('jquery-file-upload-middleware'); |
17 |
| - |
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 |
| - |
32 |
| - app.configure(function () { |
33 |
| - ... |
34 |
| - app.use('/upload', upload.fileHandler()); |
35 |
| - app.use(express.bodyParser()); |
36 |
| - ... |
37 |
| - }); |
38 |
| - |
39 |
| -``` |
40 |
| - |
41 |
| -On the frontend: |
42 |
| - |
43 |
| -```html |
44 |
| - <input id="fileupload" type="file" name="files[]" data-url="/upload" multiple> |
45 |
| - <script>$('#fileupload').fileupload({ dataType: 'json' })</script> |
46 |
| -``` |
47 |
| - |
48 |
| -To prevent access to /upload except for post (for security) |
49 |
| -```javascript |
50 |
| -upload.configure({ |
51 |
| - uploadDir: __dirname + '/public/uploads/', |
52 |
| - uploadUrl: '/uploads' |
53 |
| -}); |
54 |
| - |
55 |
| -/// Redirect all to home except post |
56 |
| -app.get('/upload', function( req, res ){ |
57 |
| - res.redirect('/'); |
58 |
| -}); |
59 |
| - |
60 |
| -app.put('/upload', function( req, res ){ |
61 |
| - res.redirect('/'); |
62 |
| -}); |
63 |
| - |
64 |
| -app.delete('/upload', function( req, res ){ |
65 |
| - res.redirect('/'); |
66 |
| -}); |
67 |
| - |
68 |
| -app.use('/upload', function(req, res, next){ |
69 |
| - upload.fileHandler({ |
70 |
| - uploadDir: function () { |
71 |
| - return __dirname + '/public/uploads/' |
72 |
| - }, |
73 |
| - uploadUrl: function () { |
74 |
| - return '/uploads' |
75 |
| - } |
76 |
| - })(req, res, next); |
77 |
| -}); |
78 |
| -``` |
79 |
| - |
80 |
| -Overriding global configuration |
81 |
| - |
82 |
| -```javascript |
83 |
| - |
84 |
| - app.use('/upload2', upload.fileHandler({ |
85 |
| - uploadDir: __dirname + '/public/uploads2', |
86 |
| - uploadUrl: '/uploads2', |
87 |
| - imageVersions: { |
88 |
| - thumbnail: { |
89 |
| - width: 100, |
90 |
| - height: 100 |
91 |
| - } |
92 |
| - } |
93 |
| - })); |
94 |
| - |
95 |
| -``` |
96 |
| - |
97 |
| -More sophisticated example - Events |
98 |
| - |
99 |
| -```javascript |
100 |
| - app.use('/upload', upload.fileHandler()); |
101 |
| - |
102 |
| - // events |
103 |
| - upload.on('begin', function (fileInfo, req, res) { |
104 |
| - // fileInfo structure is the same as returned to browser |
105 |
| - // { |
106 |
| - // name: '3 (3).jpg', |
107 |
| - // originalName: '3.jpg', |
108 |
| - // size: 79262, |
109 |
| - // type: 'image/jpeg', |
110 |
| - // delete_type: 'DELETE', |
111 |
| - // delete_url: 'http://yourhost/upload/3%20(3).jpg', |
112 |
| - // url: 'http://yourhost/uploads/3%20(3).jpg', |
113 |
| - // thumbnail_url: 'http://youhost/uploads/thumbnail/3%20(3).jpg' |
114 |
| - // } |
115 |
| - }); |
116 |
| - upload.on('abort', function (fileInfo, req, res) { ... }); |
117 |
| - upload.on('end', function (fileInfo, req, res) { ... }); |
118 |
| - upload.on('delete', function (fileInfo, req, res) { ... }); |
119 |
| - upload.on('error', function (e, req, res) { |
120 |
| - console.log(e.message); |
121 |
| - }); |
122 |
| -``` |
123 |
| - |
124 |
| -Dynamic upload directory and url, isolating user files: |
125 |
| - |
126 |
| -```javascript |
127 |
| - upload.configure({ |
128 |
| - imageVersions: { |
129 |
| - thumbnail: { |
130 |
| - width: 80, |
131 |
| - height: 80 |
132 |
| - } |
133 |
| - } |
134 |
| - }); |
135 |
| - |
136 |
| - app.use('/upload', function (req, res, next) { |
137 |
| - // imageVersions are taken from upload.configure() |
138 |
| - upload.fileHandler({ |
139 |
| - uploadDir: function () { |
140 |
| - return __dirname + '/public/uploads/' + req.sessionID |
141 |
| - }, |
142 |
| - uploadUrl: function () { |
143 |
| - return '/uploads/' + req.sessionID |
144 |
| - } |
145 |
| - })(req, res, next); |
146 |
| - }); |
147 |
| -``` |
148 |
| - |
149 |
| -Moving uploaded files to different dir: |
150 |
| - |
151 |
| -```javascript |
152 |
| - app.use('/api', function (req, res, next) { |
153 |
| - req.filemanager = upload.fileManager(); |
154 |
| - next(); |
155 |
| - }); |
156 |
| - |
157 |
| - app.use('/api/endpoint', function (req, res, next) { |
158 |
| - // your real /api handler that will actually move the file |
159 |
| - ... |
160 |
| - // req.filemanager.move(filename, path, function (err, result)) |
161 |
| - req.filemanager.move('SomeFile.jpg', 'project1', function (err, result) { |
162 |
| - // SomeFile.jpg gets moved from uploadDir/SomeFile.jpg to |
163 |
| - // uploadDir/project1/SomeFile.jpg |
164 |
| - // if path is relative (no leading slash), uploadUrl will |
165 |
| - // be used to generate relevant urls, |
166 |
| - // for absolute paths urls are not generated |
167 |
| - if (!err) { |
168 |
| - // result structure |
169 |
| - // { |
170 |
| - // filename: 'SomeFile.jpg', |
171 |
| - // url: '/uploads/project1/SomeFile.jpg', |
172 |
| - // thumbail_url : '/uploads/project1/thumbnail/SomeFile.jpg' |
173 |
| - // } |
174 |
| - ... |
175 |
| - } else { |
176 |
| - console.log(err); |
177 |
| - } |
178 |
| - }); |
179 |
| - }); |
180 |
| -``` |
181 |
| - |
182 |
| -Moving uploaded files out of uploadDir: |
183 |
| - |
184 |
| -``` |
185 |
| - app.use('/api', function (req, res, next) { |
186 |
| - var user = db.find(...); |
187 |
| -
|
188 |
| - req.filemanager = upload.fileManager({ |
189 |
| - targetDir: __dirname + '/public/u/' + user._id, |
190 |
| - targetUrl: '/u/' + user._id, |
191 |
| - }); |
192 |
| -
|
193 |
| - // or |
194 |
| - req.filemanager = upload.fileManager({ |
195 |
| - targetDir: function () { |
196 |
| - return __dirname + '/public/u/' + user._id |
197 |
| - }, |
198 |
| - targetUrl: function () { |
199 |
| - return'/u/' + user._id |
200 |
| - } |
201 |
| - }); |
202 |
| - ... |
203 |
| - req.filemanager.move(req.body.filename, 'profile', function (err, result) { |
204 |
| - // file gets moved to __dirname + '/public/u/' + user._id + '/profile' |
205 |
| - if (!err) { |
206 |
| -
|
207 |
| - } |
208 |
| - }); |
209 |
| - }); |
210 |
| -``` |
211 |
| - |
212 |
| -Getting uploaded files mapped to their fs locations: |
213 |
| - |
214 |
| -```javascript |
215 |
| - app.use('/list', function (req, res, next) { |
216 |
| - upload.fileManager().getFiles(function (files) { |
217 |
| - // { |
218 |
| - // "00001.MTS": { |
219 |
| - // "path": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/00001.MTS" |
220 |
| - // }, |
221 |
| - // "DSC00030.JPG": { |
222 |
| - // "path": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/DSC00030.JPG", |
223 |
| - // "thumbnail": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/thumbnail/DSC00030.JPG" |
224 |
| - // } |
225 |
| - // } |
226 |
| - res.json(files); |
227 |
| - }); |
228 |
| - }); |
229 |
| - |
230 |
| - // with dynamic upload directories |
231 |
| - |
232 |
| - app.use('/list', function (req, res, next) { |
233 |
| - upload.fileManager({ |
234 |
| - uploadDir: function () { |
235 |
| - return __dirname + '/public/uploads/' + req.sessionID |
236 |
| - }, |
237 |
| - uploadUrl: function () { |
238 |
| - return '/uploads/' + req.sessionID |
239 |
| - } |
240 |
| - }).getFiles(function (files) { |
241 |
| - res.json(files); |
242 |
| - }); |
243 |
| - }); |
244 |
| -``` |
245 |
| - |
246 |
| -Other options and their default values: |
247 |
| - |
248 |
| -```javascript |
249 |
| -{ |
250 |
| - tmpDir: '/tmp', |
251 |
| - uploadDir: __dirname + '/public/uploads', |
252 |
| - uploadUrl: '/uploads', |
253 |
| - targetDir: uploadDir, |
254 |
| - targetUrl: uploadUrl, |
255 |
| - ssl: false, |
256 |
| - hostname: null, // in case your reverse proxy doesn't set Host header |
257 |
| - // eg 'google.com' |
258 |
| - maxPostSize: 11000000000, // 11 GB |
259 |
| - minFileSize: 1, |
260 |
| - maxFileSize: 10000000000, // 10 GB |
261 |
| - acceptFileTypes: /.+/i, |
262 |
| - imageTypes: /\.(gif|jpe?g|png)$/i, |
263 |
| - imageVersions: { |
264 |
| - thumbnail: { |
265 |
| - width: 80, |
266 |
| - height: 80 |
267 |
| - } |
268 |
| - }, |
269 |
| - imageArgs: ['-auto-orient'], |
270 |
| - accessControl: { |
271 |
| - allowOrigin: '*', |
272 |
| - allowMethods: 'OPTIONS, HEAD, GET, POST, PUT, DELETE' |
273 |
| - } |
274 |
| -} |
275 |
| -``` |
276 |
| - |
277 |
| -## Contributors |
278 |
| - |
279 |
| - * [@soomtong](http://github.com/soomtong) |
280 |
| - * [@gsarwohadi](https://github.com/gsarwohadi) |
281 |
| - * [@peecky](https://github.com/peecky) |
282 |
| - * [@tonyspiro](https://github.com/tonyspiro) |
283 |
| - * [@derjust](https://github.com/derjust) |
284 |
| - |
285 |
| -## License |
286 |
| -Copyright (c) 2012 [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) |
287 |
| -Released under the [MIT license](http://www.opensource.org/licenses/MIT). |
| 12 | +This repos only have the fix for issues when running with Node v+7 |
| 13 | ++ delete : uncaughtException: Callback must be a function |
0 commit comments