How to send an image file over a HttpServer in Dart?
dart
Solution
When you receive a request for an image, send a header to state the content type and length, and then the file contents.
import 'dart:io';
void main() {
HttpServer.bind('127.0.0.1', 8080).then((server) {
server.listen((HttpRequest request) {
File image = new File("chicken.jpeg");
image.readAsBytes().then(
(raw){
request.response.headers.set('Content-Type', 'image/jpeg');
request.response.headers.set('Content-Length', raw.length);
request.response.add(raw);
request.response.close();
});
});
});
}
Problem
I am writing a web server application using Dart. How do I send an image file through a HttpServer to the browser?