You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module makes uploading forms nicer to deal with. In particlar, it turns `type="file"` elements into a native Stream (with backpressure and all) on the server-side so you can then simply pipe them.
6
+
7
+
First, use [utilise/form](https://github.com/utilise/utilise#--form) to turn your form element into a simple plain JSON object.
8
+
9
+
```js
10
+
const { values } =form(formElement)
11
+
```
12
+
13
+
Then upload. You get two events for updates: `progress` and `response`.
14
+
15
+
```js
16
+
ripple.upload('resource', values)
17
+
.on('progress', progress=>...)
18
+
.on('progress', response=>...)
19
+
```
20
+
21
+
The `resource` identifies which handler to use on the server-side. For example, let's say you registered an `events` resource, then its `from` function would receive the upload. Complete example:
22
+
23
+
```html
24
+
<form>
25
+
<inputtype="text"name="title">
26
+
<inputtype="file"name="photos">
27
+
</form>
28
+
```
29
+
30
+
```js
31
+
32
+
// server
33
+
ripple('events', [], { from })
34
+
35
+
functionfrom(req, res){
36
+
if (!req.type=='upload') return
37
+
38
+
// req.value contains everything you need to process
39
+
req.value== {
40
+
name:...// some text
41
+
photos: [...] // An array of streams, one for each file
42
+
}
43
+
44
+
// Use res to respond directly to the upload.
45
+
// You can pass any arguments you like.
46
+
// This is what the "response" event on the client receives
47
+
if (success)
48
+
res(200, '/event/123')
49
+
else
50
+
res(500, 'error')
51
+
}
52
+
53
+
// client
54
+
const { values } =form(formElement)
55
+
56
+
ripple.upload('events', values)
57
+
.on('progress', progress=> {
58
+
swal({
59
+
title:'Uploading Event..'
60
+
, content: progress +'%'
61
+
, type:'working'
62
+
})
63
+
})
64
+
.on('response', (status, url) => {
65
+
consttitle= status ==200?'Done':'Error'
66
+
, type = status ==200?'success':'error'
67
+
, content = status ==200
68
+
?'Great - Your event has now been published!'
69
+
:'Uh oh, something went wrong! Please try again later'
70
+
, buttons = [{ type:'primary', text:'Close' }]
71
+
72
+
go(url)
73
+
swal({ title, type, content, buttons })
74
+
})
75
+
```
76
+
77
+
(NB: `swal` comes from [pemrouz/sweet-alert](https://github.com/pemrouz/sweet-alert) and `go` from [pemrouz/decouter](https://github.com/pemrouz/decouter))
0 commit comments