Skip to content

Parsing Multipart form

Ravi Teja Gudapati edited this page Jan 9, 2019 · 16 revisions

multipart/form-data is usually used to upload forms with file fields. Jaguar has built-in support to parse HTTP requests with content type multipart/form-data. bodyAsFormData method on Context object parses the body and returns the form-data as Map<String, FormField>.

Future<Response> updateBook(Context ctx) async {
  Map<String, FormField> body = await ctx.bodyAsFormData();
  ...
}

Form fields

bodyAsFormData can decode 3 types of form-data fields:

  1. StringFormField
    Field has String value
  2. BinaryFileFormField
    Field has Stream<List<int>> value
  3. TextFileFormField
    Field has Stream<String> value

BinaryFileFormField example

Future<Redirect> upload(Context ctx) async {
  final Map<String, FormField> formData = await ctx.bodyAsFormData();
  final BinaryFileFormField pic = formData['pic'];
  await pic.writeTo('bin/data/' + pic.filename);
  return Redirect(Uri.parse("/"));
}

Example projects

  1. Upload files
    Example showing how to leverage bodyAsFormData and BinaryFormField to upload files to the server.

What's next?

In the next article, we will learn about Sessions and managing sessions in Jaguar.

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally