2014-12-18 09:25:33 +01:00
|
|
|
|
.. _-getFromFile-:
|
|
|
|
|
|
|
|
|
|
|
|
getFromFile
|
|
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
|
|
Signature
|
|
|
|
|
|
---------
|
|
|
|
|
|
|
2015-06-19 15:35:24 +02:00
|
|
|
|
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala
|
2014-12-18 09:25:33 +01:00
|
|
|
|
:snippet: getFromFile
|
|
|
|
|
|
|
|
|
|
|
|
Description
|
|
|
|
|
|
-----------
|
|
|
|
|
|
|
2015-10-08 16:26:29 +02:00
|
|
|
|
Allows exposing a file to be streamed to the client issuing the request.
|
2014-12-18 09:25:33 +01:00
|
|
|
|
|
2015-10-08 16:26:29 +02:00
|
|
|
|
The ``unmatchedPath`` (see :ref:`-extractUnmatchedPath-`) of the ``RequestContext`` is first transformed by
|
|
|
|
|
|
the given ``pathRewriter`` function, before being appended to the given directory name to build the final file name.
|
|
|
|
|
|
|
|
|
|
|
|
To files from a given directory use :ref:`-getFromDirectory-`.
|
|
|
|
|
|
To serve browsable directory listings use :ref:`-getFromBrowseableDirectories-`.
|
|
|
|
|
|
To serve files from a classpath directory use :ref:`-getFromResourceDirectory-` instead.
|
2014-12-18 09:25:33 +01:00
|
|
|
|
|
2015-10-01 13:25:41 +02:00
|
|
|
|
Note that it's not required to wrap this directive with ``get`` as this directive will only respond to ``GET`` requests.
|
|
|
|
|
|
|
2015-10-08 16:26:29 +02:00
|
|
|
|
.. note::
|
2015-11-14 22:42:22 +01:00
|
|
|
|
The file's contents will be read using an Akka Streams `Source` which *automatically uses
|
2015-10-08 16:26:29 +02:00
|
|
|
|
a pre-configured dedicated blocking io dispatcher*, which separates the blocking file operations from the rest of the stream.
|
|
|
|
|
|
|
2015-11-14 22:42:22 +01:00
|
|
|
|
Note also that thanks to using Akka Streams internally, the file will be served at the highest speed reachable by
|
2015-10-08 16:26:29 +02:00
|
|
|
|
the client, and not faster – i.e. the file will *not* end up being loaded in full into memory before writing it to
|
|
|
|
|
|
the client.
|
|
|
|
|
|
|
2015-10-01 13:25:41 +02:00
|
|
|
|
Example
|
|
|
|
|
|
-------
|
|
|
|
|
|
|
2015-10-08 16:26:29 +02:00
|
|
|
|
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala
|
|
|
|
|
|
:snippet: getFromFile-examples
|