2680

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. (http://python-eve.org/features.html#file-storage). 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.

Answer1:

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

Answer2:

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) {
        sendDatas(reader.result);
      });
      reader.readAsDataURL(file);
    }
  });
}

/// 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");
    }
  });
  req.open("POST", "http://127.0.0.1:8080/upload");
  req.send(data);
}

    

Recommend

  • How to post ajax one after another with one submit button?
  • MVC5 Ajax.BeginForm upload form with files
  • Is Phonegap's FileTransfer object obsolete with XmlHttpRequest2?
  • Excel VBA: Dynamic Variable Name
  • FormData how to get or set boundary in multipart/form-data - Angular
  • Has Angular.dart directive an equivalent to AngularJS's `restrict`
  • double actuating onblur event
  • Jquery Ajax form using .on(“submit”,
  • Dart HTTP server and Futures
  • How to get or calculate size of Azure File/Share or Service
  • Where to save the local DB created for iphone app?
  • css font-size and line-height not matching the baseline
  • get passwords from chrome
  • Graceful pod termination
  • insert a picture into database(sqlite) with java code. what should i do?
  • Does the Azure table storage API cache results?
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • Better Indy for Dephi 2007
  • Compare struct to a constant in C
  • Activation Function choice for Neural network
  • Cannot get text from text area
  • Dart - Isolate Cross Window Communication
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • Does Mobilefirst provide a provision to access web services directly?
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Updating server-side rendering client-side
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • How to pass list parameters for each object using Spring MVC?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • reshape alternating columns in less time and using less memory