Handling multipart uploads with Aqueduct 3.0

643 views Asked by At

I'm using AngularDart on client side, and Aqueduct 3.0 on API/Server side. I would like to upload files and store them on the filesystem for the moment.

Is there a recommended way how Aqueduct can receive files / multipart form requests and store them locally?

I have been looking at several other server frameworks, but all have been built for Dart 1 and not Dart 2. Before I start figuring out, how to write an own server for the purpose to receive and store files, I thought I'll ask here first, as I have seen that there was somebody requesting it on GitHub with #315.

In Angular, the upload function would look as following below (file_uploader.dart)

import 'dart:html';

import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';

/// AngularDart component for uploading files
// https://gist.github.com/alexd1971/67f4502c5e0d594d30f901fb0559fd7e
@Component(
  selector: 'file-uploader',
  templateUrl: 'file_uploader.html',
  directives: [coreDirectives, formDirectives]
)
class FileUploader {
  String progress;
  void uploadFiles(form) {
    var formData = new FormData(form);
    final request = new HttpRequest();
    request.open('POST', 'http://localhost:9999/upload');
    request.upload.onProgress.listen((ProgressEvent e) {
      progress = (e.loaded*100/e.total).toInt().toString() + '%';
    });
    request.onLoad.listen((e) {
      print('Uploaded');
    });
    request.send(formData);
  }
}
1

There are 1 answers

0
Joe Conway On BEST ANSWER

This feature is in the queue, and we can start work once Dart 2 stuff simmers down. It's also something you can extend the framework to do in your project by creating a Codec subclass (from the standard lib dart:convert) that encodes and decodes multipart form data. You register that codec for the the content type during startup:

CodecRegistry.add(ContentType("multipart/mixed"), MultipartCodec())

For an example implementation of a codec, see application/x-www-form-urlencoded in the source.