fromUrl static method

Future<bool> fromUrl(
  1. String url, {
  2. String? name,
  3. String? mime,
  4. SaveFileExtensionBehavior extensionBehavior = SaveFileExtensionBehavior.replace,
})

Downloads file from the provided url and saves it to a location where it's accessible by the user.

The url will be downloaded by executing a GET request without any additional headers. Returns true if the file has been saved and false if user has cancelled the action (can happen only on Android). Throws if there's been an error.

If mime or name parameters are not provided, they will be inferred using the following methods, in order: For mime:

  1. Content-Type header returned via a HEAD request.
  2. lookupMimeType function using the last path segment. For name:
  3. Content-Disposition header returned via a HEAD request.
  4. Last path segment.

The extensionBehavior parameter determines what to do if the extension of provided or inferred name does not match the provided or inferred mime type. See SaveFileExtensionBehavior for documentation of the available options.

On the Web, when url is cross-origin, server should send the Content-Disposition: attachment header to prevent the browser from blocking the request. However, if the filename parameter is also sent in the header, it will override the name parameter.

On the Web, method will complete immediately and start the download in the background, without the ability to monitor its status. To gain more control over this process, consider using HttpRequest.request with responseType: 'blob' and then UtopiaSaveFileWeb.fromBlob.

Implementation

static Future<bool> fromUrl(
  String url, {
  String? name,
  String? mime,
  SaveFileExtensionBehavior extensionBehavior = SaveFileExtensionBehavior.replace,
}) async {
  final metadata = await MetadataHelper.fromUrl(url, mime: mime, name: name);
  return _impl.fromUrl(url, ExtensionHelper.ensureValid(metadata, extensionBehavior));
}