Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Downloading a file


This recipe shows you the simplest ways to download a file through code, first in a command-line application and then from a web application. As an example, we download the front page of the Learning Dart website from http://learningdart.org.

Getting ready

A client program (be it web or command-line) receives content, such as files or web pages, from a web server using the HTTP protocol. The dart:html and dart:io package provides us with the basic classes we need to do this, which are as follows:

  • The Uriclass class (from dart:core) has all we need to parse, encode, and decode web addresses; the method Uri.parse is often used

  • The HttpRequest class (from dart:html) has the getString method to fetch a file from a URL

  • The HttpClientclass class (from dart:io) has all kinds of methods, such as get and post, to send a request (class HttpClientRequest) to a web server and get a response (class HttpClientResponse) back

How to do it...

  1. For a web app, this is shown in download_string.dart...