简体   繁体   English

如何从 sql 数据库中的单个单元格下载多个文件? 我正在使用 laravel 5.5

[英]how to download multiple files from a single cell in sql database? I am using laravel 5.5

I have a user row with employee's details and $image has multiple files stored in database.我有一个包含员工详细信息的用户行,并且 $image 有多个文件存储在数据库中。 I want to download each file.我想下载每个文件。 so far I can only download a single file stored in $image到目前为止,我只能下载存储在 $image 中的单个文件

here is myview.blade.php这是myview.blade.php

<input type="file" name="image[]" class="form-control" value="{{ $employee['image'] }}" multiple>
 <a href="{{ url('/people/employees/download/' . $employee['image']) }}">{{$employee['image']}}</a>

my route.php我的route.php

Route::get('/people/employees/upload/{id}', 'EmplController@upload');
Route::get('/people/employees/download/{image}', 'EmplController@download');

my controller.php我的controller.php

public function test(Request $request, $id) {
    
    $employee = User::find($id);

    if($request->hasfile('image')){
        $files = [];
        foreach ($request->image as $image) {
            $path = $image->getClientOriginalName();
            $filename = time() . '-' . $path;
            $files[] = $filename;
            $image->storeAs('employees', $employee->id . '/' . $filename);
            $image->move(public_path('employees'),$filename);
        }

        $files = explode(",", $files[0]);
    }

    $employee->image = $employee->image  .  $files[0];
    $employee->save();  
}

public function download($image){
    $employee = User::find($image);
    $filename = $image;
    $filepath = public_path('employees/' . $filename);

    return response()->download($filepath);
}

when I do a print_r function while uploading multiple files it picks one file?当我在上传多个文件时执行 print_r function 时会选择一个文件?

Array ( [0] => 1596838646-logo.jpg )

I want to download multiple files displayed on myview.blade.我想下载 myview.blade 上显示的多个文件。

I believe that what you asking is to download multiple files over the same request using HTTP protocol and that is not possible as it is explained in this answer:我相信您要问的是使用 HTTP 协议通过同一请求下载多个文件,这是不可能的,因为它在这个答案中解释:

Laravel Download Multiple Files Laravel 下载多个文件

What I would suggest is to make a client-side function that can make multiple calls to the download method for each specific file.我的建议是制作一个客户端 function 可以对每个特定文件的下载方法进行多次调用。

Eg based on your question, It could be something like:例如,根据您的问题,可能类似于:

HTML: HTML:

<a onclick='return downloadFiles()'>{{$employee['image']}}</a>

JS: JS:

function downloadFiles(e) {
    e.preventDefault();
    for (img of IMAGES_FROM_THE_BACK) { 
        setTimeout(() => {
            window.open(`/people/employees/download/${img}`);
        }, 300);
    }
});

Please note that you would need to pass your images to an array or object from the back to the front end where you've your HTML.请注意,您需要将图像从后到前端传递到数组或 object 中,这样您就有了 HTML。

As the subop from above said It's impossible, because HTTP doesn't support multiple files download.正如上面的子操作所说,这是不可能的,因为 HTTP 不支持多个文件下载。 And one of solution is to open connection per file to download, but in my opinion more elegant would be files zipping which has them all.解决方案之一是打开每个文件的连接以下载,但在我看来,更优雅的是文件压缩,它包含所有文件。

PHP provides solution for that and it's ZipArchive PHP 提供了解决方案,它是ZipArchive

After, you ZIP all files, write on disk in tmp directory, and send a stream response using your file location.之后,您 ZIP 所有文件,写入磁盘的 tmp 目录中,并使用您的文件位置发送 stream 响应。 After php close connection, you can use Laravel Terminate Middleware/Callback and delete that archive to flush space on your disk.在 php 关闭连接后,您可以使用Laravel 终止中间件/回调并删除该存档以刷新磁盘上的空间。

Simple example in plain PHP taken from link above.从上面的链接中获取的普通 PHP 中的简单示例。

$files = array('image.jpeg','text.txt','music.wav');
$zipname = 'enter_any_name_for_the_zipped_file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

///Then download the zipped file.
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次上传新文件时,如何将保存的文件保存在单元格中? 我正在使用 laravel 5.5 - how can i keep saved files in a cell everytime i upload a new file? am using laravel 5.5 如何从数据库中删除多条记录(使用 laravel 5.5) - how to delete multiple records from database ( using laravel 5.5) Laravel 5.5多个数据库连接 - Laravel 5.5 multiple database connection Laravel 5.5:我是否在正确的模式下使用依赖注入? - Laravel 5.5: Am I using Dependency Injection in right mode? 我应该将包含正在使用的记录的sql文件放在laravel项目层次结构文件夹中的数据库Seeders中的什么位置 - Where should I place my .sql files containing records that I am using in my Database Seeders in laravel project hierarchy folder Laravel:通过使用“With”,我是否避免了对数据库的多个查询/连接? - Laravel: by using "With" am I avoiding multiple queries/connections into database? 如何在Laravel 5.5中的单个控制器中从.env写入和重新加载变量 - How to write and reload variables from .env in a single controller in Laravel 5.5 如何从laravel 5.4中的数据库下载多个图像 - How to download multiple image from database in laravel 5.4 Laravel 5.5单视图上的多个控制器 - Laravel 5.5 multiple controller on single view Laravel 5.2-从数据库下载文件 - Laravel 5.2 - Download Files From Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM