mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: add another vweb example, showing file upload, transform, and file download (#14842)
This commit is contained in:
parent
ccc3271493
commit
d336b7b877
80
examples/vweb/file_transform/index.html
Normal file
80
examples/vweb/file_transform/index.html
Normal 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>
|
5
examples/vweb/file_transform/sample_input.txt
Normal file
5
examples/vweb/file_transform/sample_input.txt
Normal file
@ -0,0 +1,5 @@
|
||||
10 13
|
||||
20 54
|
||||
30 82
|
||||
40 11
|
||||
50 47
|
2
examples/vweb/file_transform/upload.html
Normal file
2
examples/vweb/file_transform/upload.html
Normal file
@ -0,0 +1,2 @@
|
||||
<meta charset="utf-8">
|
||||
<a href="/">Back</a>
|
40
examples/vweb/file_transform/vweb_example.v
Normal file
40
examples/vweb/file_transform/vweb_example.v
Normal 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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user