@@ -406,13 +406,15 @@ represented by a PHP callable instead of a string::
406
406
});
407
407
$response->send();
408
408
409
- Downloading Files
410
- ~~~~~~~~~~~~~~~~~
409
+ .. _component-http-foundation-serving-files :
410
+
411
+ Serving Files
412
+ ~~~~~~~~~~~~~
411
413
412
414
.. versionadded :: 2.1
413
415
The ``makeDisposition `` method was added in Symfony 2.1.
414
416
415
- When uploading a file, you must add a ``Content-Disposition `` header to your
417
+ When sending a file, you must add a ``Content-Disposition `` header to your
416
418
response. While creating this header for basic file downloads is easy, using
417
419
non-ASCII filenames is more involving. The
418
420
:method: `Symfony\\ Component\\ HttpFoundation\\ Response::makeDisposition `
@@ -424,6 +426,26 @@ abstracts the hard work behind a simple API::
424
426
425
427
$response->headers->set('Content-Disposition', $d);
426
428
429
+ .. versionadded :: 2.2
430
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `
431
+ class was added in Symfony 2.2.
432
+
433
+ Alternatively, if you are serving a static file, you can use a
434
+ :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `::
435
+
436
+ use Symfony\Component\HttpFoundation\BinaryFileResponse
437
+
438
+ $file = 'path/to/file.txt';
439
+ $response = new BinaryFileResponse($file);
440
+
441
+ The ``BinaryFileResponse `` will automatically handle ``Range `` and
442
+ ``If-Range `` headers from the request. You can also set the ``Content-Type ``
443
+ of the sent file, or change its ``Content-Disposition ``::
444
+
445
+ $response->headers->set('Content-Type', 'text/plain')
446
+ $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'filename.txt');
447
+
448
+
427
449
.. _component-http-foundation-json-response :
428
450
429
451
Creating a JSON Response
@@ -442,7 +464,8 @@ right content and headers. A JSON response might look like this::
442
464
$response->headers->set('Content-Type', 'application/json');
443
465
444
466
.. versionadded :: 2.1
445
- The :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse ` class was added in Symfony 2.1.
467
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `
468
+ class was added in Symfony 2.1.
446
469
447
470
There is also a helpful :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `
448
471
class, which can make this even easier::
0 commit comments