Introduction to Laravel5 file download

Source: Internet
Author: User
Brief introduction to Laravel5 download file Laravel 5 download file

Laravel provides many methods for downloading files. you can use Stroage. Laravel 5's own file system is very powerful. It provides many methods and supports remote storage. You can also use Response directly. laravel 5's Response is also based onSyfmonyFurther encapsulation of Symfony \ Component \ HttpFoundation \ Response.

Practice truth

Yesterday I made a file download function. I thought it was necessary to use Stroage to complete all operations. I checked the official website documentation and found that the download method of Response was used to download the file, in this case, why not directly use the download method of Response? Of course, the premise is that I already know the absolute or relative path of the file.

The default Storage path is in the/storage/app Directory. if you do not set the path, you can use the method in Storage to obtain or save files in this directory.

But the file I saved is not in this directory. if there is an absolute path, the easiest way is to directly use the download method.

If it is a remote ftp, you have to use Storage, select the ftp service, get it, and pass in the Response: download () method. the usage is similar to the following.

return response()->download($resource['path'] . $resource['name'], $resource['original_name']);

Let's take a look at the download method:

 /**  * Create a new file download response.  *   * @param  \SplFileInfo|string  $file  * @param  string  $name  * @param  array  $headers  * @param  string|null  $disposition  * @return \Symfony\Component\HttpFoundation\BinaryFileResponse  */  
public function download($file, $name = null, array $headers = [], $disposition = 'attachment');
  • The first is the absolute path of the file or the file (required) \ SplFileInfo or string type.
  • The second is the file name saved after Download (optional). If this parameter is left blank, the file name saved after download will be of the same string type as the file name saved on the server.
  • The second is the header (optional) array type.
  • What is the third description? I have never tried this either.

In this case, it's so easy to download files. let's simply write it. there are too many laravel tutorials online. In fact, I don't really want to write laravel, because I don't like this framework very much, it is very convenient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.