examples: add another vweb example, showing file upload, transform, and file download (#14842)

master
CC 2022-06-24 00:23:47 -06:00 committed by GitHub
parent ccc3271493
commit d336b7b877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test file uploading data transformation</title>
</head>
<body>
<h2>File Upload and Data Transformation</h2>
Upload the <b>sample_input.txt</b> located in this example folder.
<br>
<br>
V will apply data transformation, adding the first two columns into a third column, and return the results as a download. <br>
<br>
For example:
<table style="color:blue;">
<tr>
<td>10</td>
<td>13</td>
</tr>
<tr>
<td>20</td>
<td>54</td>
</tr>
<tr>
<td>30</td>
<td>82</td>
</tr>
<tr>
<td>40</td>
<td>11</td>
</tr>
<tr>
<td>50</td>
<td>47</td>
</tr>
</table>
<br>
Becomes...
<br>
<table style="color:blue;">
<tr>
<td>10</td>
<td>13</td>
<td>23</td>
</tr>
<tr>
<td>20</td>
<td>54</td>
<td>74</td>
</tr>
<tr>
<td>30</td>
<td>82</td>
<td>112</td>
</tr>
<tr>
<td>40</td>
<td>11</td>
<td>51</td>
</tr>
<tr>
<td>50</td>
<td>47</td>
<td>97</td>
</tr>
</table>
<hr>
File form:
<form method="POST" enctype="multipart/form-data" action="http://localhost:8082/upload">
<input type="file" name="upfile"/>
<input type="submit" value="Upload & Transform"/>
</form>
</body>
</html>

View File

@ -0,0 +1,5 @@
10 13
20 54
30 82
40 11
50 47

View File

@ -0,0 +1,2 @@
<meta charset="utf-8">
<a href="/">Back</a>

View File

@ -0,0 +1,40 @@
module main
import vweb
const port = 8082
struct App {
vweb.Context
}
fn main() {
vweb.run(&App{}, port)
}
pub fn (mut app App) index() vweb.Result {
return $vweb.html()
}
['/upload'; post]
pub fn (mut app App) upload() vweb.Result {
fdata := app.files['upfile']
data_rows := fdata[0].data.split('\n')
mut output_data := ''
for elem in data_rows {
delim_row := elem.split('\t')
output_data += '${delim_row[0]}\t${delim_row[1]}\t${delim_row[0].int() + delim_row[1].int()}\n'
}
output_data = output_data.all_before_last('\n')
println(output_data)
app.add_header('Content-Disposition', 'attachment; filename=results.txt')
app.send_response_to_client('application/octet-stream', output_data)
return $vweb.html()
}