mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb: add multipart/form-data parser and file upload (#8160)
This commit is contained in:
6
examples/vweb/file_upload/index.html
Normal file
6
examples/vweb/file_upload/index.html
Normal file
@ -0,0 +1,6 @@
|
||||
<h2>File Upload</h2>
|
||||
|
||||
<form method="POST" enctype="multipart/form-data" action="/upload">
|
||||
<input type="file" name="upfile" multiple>
|
||||
<input type="submit" value="Press">
|
||||
</form>
|
14
examples/vweb/file_upload/upload.html
Normal file
14
examples/vweb/file_upload/upload.html
Normal file
@ -0,0 +1,14 @@
|
||||
<meta charset="utf-8">
|
||||
|
||||
File amount: @fdata.len
|
||||
|
||||
@for i, data in fdata
|
||||
|
||||
<h2>Filename: @data.filename</h2>
|
||||
<h2>Type: @data.content_type</h2>
|
||||
|
||||
<p>@{files[i]}</p>
|
||||
|
||||
@end
|
||||
|
||||
<a href="/">Back</a>
|
33
examples/vweb/file_upload/vweb_example.v
Normal file
33
examples/vweb/file_upload/vweb_example.v
Normal file
@ -0,0 +1,33 @@
|
||||
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()
|
||||
}
|
||||
|
||||
[post]
|
||||
['/upload']
|
||||
pub fn (mut app App) upload() vweb.Result {
|
||||
fdata := app.files['upfile']
|
||||
|
||||
mut files := []vweb.RawHtml{}
|
||||
|
||||
for d in fdata {
|
||||
files << d.data.replace_each(['\n', '<br>', '\n\r', '<br>' '\t', ' ', ' ', ' '])
|
||||
}
|
||||
|
||||
return $vweb.html()
|
||||
}
|
Reference in New Issue
Block a user