Skip to content

Commit d336b7b

Browse files
authored
examples: add another vweb example, showing file upload, transform, and file download (#14842)
1 parent ccc3271 commit d336b7b

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Test file uploading data transformation</title>
6+
</head>
7+
<body>
8+
9+
<h2>File Upload and Data Transformation</h2>
10+
11+
Upload the <b>sample_input.txt</b> located in this example folder.
12+
<br>
13+
<br>
14+
V will apply data transformation, adding the first two columns into a third column, and return the results as a download. <br>
15+
<br>
16+
17+
For example:
18+
<table style="color:blue;">
19+
<tr>
20+
<td>10</td>
21+
<td>13</td>
22+
</tr>
23+
<tr>
24+
<td>20</td>
25+
<td>54</td>
26+
</tr>
27+
<tr>
28+
<td>30</td>
29+
<td>82</td>
30+
</tr>
31+
<tr>
32+
<td>40</td>
33+
<td>11</td>
34+
</tr>
35+
<tr>
36+
<td>50</td>
37+
<td>47</td>
38+
</tr>
39+
</table>
40+
<br>
41+
Becomes...
42+
<br>
43+
<table style="color:blue;">
44+
<tr>
45+
<td>10</td>
46+
<td>13</td>
47+
<td>23</td>
48+
</tr>
49+
<tr>
50+
<td>20</td>
51+
<td>54</td>
52+
<td>74</td>
53+
</tr>
54+
<tr>
55+
<td>30</td>
56+
<td>82</td>
57+
<td>112</td>
58+
</tr>
59+
<tr>
60+
<td>40</td>
61+
<td>11</td>
62+
<td>51</td>
63+
</tr>
64+
<tr>
65+
<td>50</td>
66+
<td>47</td>
67+
<td>97</td>
68+
</tr>
69+
</table>
70+
71+
<hr>
72+
73+
File form:
74+
<form method="POST" enctype="multipart/form-data" action="http://localhost:8082/upload">
75+
<input type="file" name="upfile"/>
76+
<input type="submit" value="Upload & Transform"/>
77+
</form>
78+
79+
</body>
80+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
10 13
2+
20 54
3+
30 82
4+
40 11
5+
50 47
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<meta charset="utf-8">
2+
<a href="/">Back</a>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module main
2+
3+
import vweb
4+
5+
const port = 8082
6+
7+
struct App {
8+
vweb.Context
9+
}
10+
11+
fn main() {
12+
vweb.run(&App{}, port)
13+
}
14+
15+
pub fn (mut app App) index() vweb.Result {
16+
return $vweb.html()
17+
}
18+
19+
['/upload'; post]
20+
pub fn (mut app App) upload() vweb.Result {
21+
fdata := app.files['upfile']
22+
23+
data_rows := fdata[0].data.split('\n')
24+
25+
mut output_data := ''
26+
27+
for elem in data_rows {
28+
delim_row := elem.split('\t')
29+
output_data += '${delim_row[0]}\t${delim_row[1]}\t${delim_row[0].int() + delim_row[1].int()}\n'
30+
}
31+
32+
output_data = output_data.all_before_last('\n')
33+
34+
println(output_data)
35+
36+
app.add_header('Content-Disposition', 'attachment; filename=results.txt')
37+
app.send_response_to_client('application/octet-stream', output_data)
38+
39+
return $vweb.html()
40+
}

0 commit comments

Comments
 (0)