From d336b7b877caf2c93461cd7b7d806eae9f10836b Mon Sep 17 00:00:00 2001 From: CC Date: Fri, 24 Jun 2022 00:23:47 -0600 Subject: [PATCH] examples: add another vweb example, showing file upload, transform, and file download (#14842) --- examples/vweb/file_transform/index.html | 80 +++++++++++++++++++ examples/vweb/file_transform/sample_input.txt | 5 ++ examples/vweb/file_transform/upload.html | 2 + examples/vweb/file_transform/vweb_example.v | 40 ++++++++++ 4 files changed, 127 insertions(+) create mode 100644 examples/vweb/file_transform/index.html create mode 100644 examples/vweb/file_transform/sample_input.txt create mode 100644 examples/vweb/file_transform/upload.html create mode 100644 examples/vweb/file_transform/vweb_example.v diff --git a/examples/vweb/file_transform/index.html b/examples/vweb/file_transform/index.html new file mode 100644 index 000000000..1a36016d9 --- /dev/null +++ b/examples/vweb/file_transform/index.html @@ -0,0 +1,80 @@ + + + + +Test file uploading data transformation + + + +

File Upload and Data Transformation

+ +Upload the sample_input.txt located in this example folder. +
+
+V will apply data transformation, adding the first two columns into a third column, and return the results as a download.
+
+ +For example: + + + + + + + + + + + + + + + + + + + + + +
1013
2054
3082
4011
5047
+
+Becomes... +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
101323
205474
3082112
401151
504797
+ +
+ +File form: +
+ + +
+ + + diff --git a/examples/vweb/file_transform/sample_input.txt b/examples/vweb/file_transform/sample_input.txt new file mode 100644 index 000000000..030d5f622 --- /dev/null +++ b/examples/vweb/file_transform/sample_input.txt @@ -0,0 +1,5 @@ +10 13 +20 54 +30 82 +40 11 +50 47 \ No newline at end of file diff --git a/examples/vweb/file_transform/upload.html b/examples/vweb/file_transform/upload.html new file mode 100644 index 000000000..8e0595554 --- /dev/null +++ b/examples/vweb/file_transform/upload.html @@ -0,0 +1,2 @@ + +Back diff --git a/examples/vweb/file_transform/vweb_example.v b/examples/vweb/file_transform/vweb_example.v new file mode 100644 index 000000000..062923537 --- /dev/null +++ b/examples/vweb/file_transform/vweb_example.v @@ -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() +} -- 2.30.2