How to send multipart HTTP requests from browser with Dart

I have to upload an image from browser to my RESTful web API, implemented using Python Eve. From documentation, It requires sending multipart/data-form request. ( There is 'dart:http' library that could do. But, it requires 'dart:io', which is not available on browser. So, is there anyway I can send the request from browser?

Thank you for any help.


You can just use something like this:

FormData formData = new FormData();
formData.append('image', '...');
HttpRequest.request('url', method: 'post', sendData: formData).then((HttpRequest request) {
  // ...

This should set the correct mimeType. If not you can set it with the mimeType parameter.

Regards, Robert


Client side, the dart:html library should do the trick. Something like this (source):

import 'dart:html';

main() {
  InputElement uploadInput = query('#upload');
  uploadInput.on.change.add((e) {
    // read file content as dataURL
    final files = uploadInput.files;
    if (files.length == 1) {
      final file = files[0];
      final reader = new FileReader();
      reader.on.load.add((e) {

/// send data to server
sendDatas(dynamic data) {
  final req = new HttpRequest();
  req.on.readyStateChange.add((Event e) {
    if (req.readyState == HttpRequest.DONE &&
        (req.status == 200 || req.status == 0)) {
      window.alert("upload complete");
  });"POST", "");


人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:How to send multipart HTTP requests from browser with Dart