Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Until a multipart encoding parser is available, you can use <a href="http://www.w3.org/TR/file-upload/" rel="nofollow noreferrer">File API</a> on client to read and upload file content as <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow noreferrer">dataUrl</a>. This API has quite good support in browsers ( see <a href="http://caniuse.com/filereader" rel="nofollow noreferrer">http://caniuse.com/filereader</a> ).</p> <p>On client side :</p> <pre class="lang-dart prettyprint-override"><code>import 'dart:html'; main() { InputElement uploadInput = query('#upload'); uploadInput.onChange.listen((e) { // read file content as dataURL final files = uploadInput.files; if (files.length == 1) { final file = files[0]; final reader = new FileReader(); reader.onLoad.listen((e) { sendDatas(reader.result); }); reader.readAsDataUrl(file); } }); } /// send data to server sendDatas(dynamic data) { final req = new HttpRequest(); req.onReadyStateChange.listen((Event e) { if (req.readyState == HttpRequest.DONE &amp;&amp; (req.status == 200 || req.status == 0)) { window.alert("upload complete"); } }); req.open("POST", "http://127.0.0.1:8080/upload"); req.send(data); } </code></pre> <p>And on server side :</p> <pre class="lang-dart prettyprint-override"><code>import 'dart:io'; main() { final server = new HttpServer(); server.listen('127.0.0.1', 8080); server.addRequestHandler((request) =&gt; request.path == '/upload' &amp;&amp; request.method.toLowerCase() == 'post' , (HttpRequest request, HttpResponse response) { _readBody(request, (body) { // handle your dataURL // example with image : data:image/jpeg;base64,/9j/4S2YRXhpZgAATU0AK... // return result response.statusCode = HttpStatus.CREATED; response.contentLength = 0; response.outputStream.close(); }); }); } /// Read body of [request] and call [handleBody] when complete. _readBody(HttpRequest request, void handleBody(String body)) { String bodyString = ""; // request body byte data final completer = new Completer(); final sis = new StringInputStream(request.inputStream, Encoding.UTF_8); sis.onData = (){ bodyString = bodyString.concat(sis.read()); }; sis.onClosed = () { completer.complete(""); }; sis.onError = (Exception e) { print('exeption occured : ${e.toString()}'); }; // process the request and send a response completer.future.then((_){ handleBody(bodyString); }); } </code></pre> <p>References :</p> <ul> <li><a href="http://www.html5rocks.com/en/tutorials/file/dndfiles/" rel="nofollow noreferrer">Reading files in JavaScript using the File APIs</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload