nzo/file-downloader-bundle

NzoFileDownloaderBundle 是一个用于从服务器和 Web 应用程序中安全且轻松地下载所有类型文件的 Symfony Bundle。您还可以在 Web 浏览器中读取/显示文件内容。

安装数: 13,385

依赖者: 0

建议者: 0

安全性: 0

星标: 8

关注者: 3

分支: 1

类型:symfony-bundle

v4.2.0 2021-12-06 08:22 UTC

This package is auto-updated.

Last update: 2024-09-06 13:57:30 UTC


README

Build Status Total Downloads Latest Stable Version

NzoFileDownloaderBundle 是一个用于从 服务器Web 应用程序项目 安全且轻松地 下载 所有类型 文件 的 Symfony Bundle。您还可以在 Web 浏览器中 读取/显示 文件内容。

功能包括

  • 本版本的包与 Symfony >= v4.4 兼容
  • 在 Web 浏览器读取/显示 文件内容。
  • 从 Symfony public 文件夹或自定义路径 下载 所有类型的 文件
  • 下载时更改文件名称。
  • 从 URL 下载文件
  • 从 URL 获取文件扩展名

安装

通过 Composer

$ composer require nzo/file-downloader-bundle

在 config/bundles.php 中注册包(无 Flex)

// config/bundles.php

return [
    // ...
    Nzo\FileDownloaderBundle\NzoFileDownloaderBundle::class => ['all' => true],
];

使用方法

在 Web 浏览器中读取/显示文件内容

use Nzo\FileDownloaderBundle\FileDownloader\FileDownloader;

class MyController extends AbstractController
{
    private $fileDownloader;

    public function __construct(FileDownloader $fileDownloader)
    {
        $this->fileDownloader = $fileDownloader;
        
        // without autowiring use: $this->get('nzo_file_downloader')
    }

// In this examples the "myfile.pdf" file exist in "public/myfolder/myfile.pdf".

     public function readFilesFromPublicFolder()
     {
          return $this->fileDownloader->readFile('myfolder/myfile.pdf');
     }

     // Absolute PATH:

     public function readFilesFromAbsolutePath()
      {
           return $this->fileDownloader->readFileFromAbsolutePath('/home/user/myfile.pdf');
      }
}    

下载文件

     public function downloadFileFromPublicFolder()
     {
          return $this->fileDownloader->downloadFile('myfolder/myfile.pdf');

        # change the name of the file when downloading:

          return $this->fileDownloader->downloadFile('myfolder/myfile.pdf', 'newName.pdf');
     }


     // Absolute PATH:

     public function downloadFilesFromAbsolutePath()
      {
           return $this->fileDownloader->downloadFileFromAbsolutePath('/home/user/myfile.pdf');

         # change the name of the file when downloading:

           return $this->fileDownloader->downloadFileFromAbsolutePath('/home/user/myfile.pdf', 'newName.pdf');
      }
}    
URL 下载文件
    public function downloadFileFromUrl(string $url, string $pathWhereToDownloadTheFile, ?string $customUserAgent = null)
    {
        $headers = ['Authorization: Basic auth'];
        
        $response =  $this->fileDownloader->downloadFileFromUrl($url, $pathWhereToDownloadTheFile, $headers, /** You can pass an optional custom User-Agent as third argument ($customUserAgent) */);
    
        if (false !== $response) {
            // File downloaded successfully !
        } else {
            // Error occurred ! 
        }   
    }
URL 获取文件扩展名
public function getFileExtensionFromUrl(string $url)
{
    $fileExtension = $this->fileDownloader->getFileExtensionFromUrl($url);

    if (null === $fileExtension) {
        // Error occurred ! 
    }
}
下载 Symfony StreamedResponse
    use Symfony\Component\HttpFoundation\StreamedResponse;

    // ...

    public function downloadStreamedResponse()
    {
        $streamedResponse = new StreamedResponse();
        // ...

        $fileName = 'someFileName.csv';

        return $this->fileDownloader->downloadStreamedResponse($streamedResponse, $fileName);
    }

许可

本包采用 MIT 许可证。请参阅包中的完整许可证。

查看 资源文档/LICENSE