Fasthttp: How to work with uploads

Created on 17 Jan 2019  路  2Comments  路  Source: valyala/fasthttp

Hello,

i am morely new to _Go_ and build some things for my backend these days with net/http. Now, i tried fasthttp which works awesome - as known 馃槉 But, it's a very wide topic and a very complex API.

I want to work with uploaded POST form values and multipart/form-data, which seems confusing. In most languages and frameworks one can iterate over this data, like an _array_ or _map_ (practically without knowing all their names itself). I did not found some samples how to parse uploads in fasthttp.


Given is my _upload-script-dummy_ as simple function:

func upload(ctx *fasthttp.RequestCtx) {
  // ...
}

This function in this case is called via https://domain.tld/upload with the following _dummy-forms_


1: simple data

<form action="upload" method="post">
    <input type="text" name="name">
    <input type="email" name="mail">
    <textarea name="message"></textarea>
</form>

How to iterate over those _pairs_? (name=max, [email protected], message=Hi!)

2: single file

<form method="upload" enctype="multipart/form-data">
    <input name="file" type="file"> 
</form>

How do i get/save (move) this (temp-)file? (file=name.jpg)
How to access extended data, like size etc.?

3: multiple files

<form method="upload" enctype="multipart/form-data">
    <input name="files[]" type="file" multiple> 
</form>

How to iterate over multiple files in files[]?
(files=[1=[name=a.jpg, size=12345, ...], 2=[name=b.png, size=...], 3=[name=c.txt, ...]])
How to access their extended data, like size, tempname or whatever is in there?


Can someone give me reduced examples (just the needed lines) to handle that?

Thank you very much 鈽猴笍

(i am happy too if it's only a sample for one of that 馃槉 )

Most helpful comment

Multipart forms are so easy to use in fasthttp. I think than are more easy than net/http. I won't provide you a full example only some utils functions.

You can use MultipartForm to interact with the multipart form (obviously).

form, err := ctx.MultipartForm()
if err != nil {
  ctx.Error(...)
  return
}
// We got the form!

Given the first html form we will make the following to get text, email and message (do not forget form.Value is a map[string][]string, so given a key the value will be a string slice):

texts := form.Value["text"]
if len(texts) == 0 {
  ctx.Error(...)
}
text := texts[0]
emails := form.Value["email"]
if len(emails) == 0 {
  ctx.Error(...)
}
email := emails[0]
...

Given the second form we will get the file using FormFile and store it in disk using SaveMultipartFile.

header, err := ctx.FormFile("file")
if err != nil {
  ctx.Error(...)
}
fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))

Given the third form we will get the files walking along form.Files map.

form, err := ctx.MultipartForm()
if err != nil {
  ctx.Error(...)
  return
}
for k, v := range form.File {
  for _, header := range v {
    fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))
  }
}

All 2 comments

Multipart forms are so easy to use in fasthttp. I think than are more easy than net/http. I won't provide you a full example only some utils functions.

You can use MultipartForm to interact with the multipart form (obviously).

form, err := ctx.MultipartForm()
if err != nil {
  ctx.Error(...)
  return
}
// We got the form!

Given the first html form we will make the following to get text, email and message (do not forget form.Value is a map[string][]string, so given a key the value will be a string slice):

texts := form.Value["text"]
if len(texts) == 0 {
  ctx.Error(...)
}
text := texts[0]
emails := form.Value["email"]
if len(emails) == 0 {
  ctx.Error(...)
}
email := emails[0]
...

Given the second form we will get the file using FormFile and store it in disk using SaveMultipartFile.

header, err := ctx.FormFile("file")
if err != nil {
  ctx.Error(...)
}
fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))

Given the third form we will get the files walking along form.Files map.

form, err := ctx.MultipartForm()
if err != nil {
  ctx.Error(...)
  return
}
for k, v := range form.File {
  for _, header := range v {
    fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))
  }
}

@dgrr thank you very much :100:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

facundomedica picture facundomedica  路  4Comments

horgh picture horgh  路  5Comments

jeremyjpj0916 picture jeremyjpj0916  路  5Comments

AnikHasibul picture AnikHasibul  路  3Comments

mayocream picture mayocream  路  3Comments