单击图像/文件名查看或下载图像

时间:2017-11-14 09:35:37

标签: laravel-5

我使用了laravel关于批量图像上传的几个教程,但他们都谈到将图像显示为缩略图,这不是我想要实现的。我想查看存储的图像文件名,并且用户也可以下载保存的图像。

控制器

<span class="error" ng-show="passwordVerify">Password not match</span>

我有一个表格,显示存储在数据库中的图像名称,现在我希望在用户点击或查看按钮时显示或下载相应的图像。

class Controller_Name extends Controller {
//index
  public function index() {
    $variable = Model::all();
    return view('your.view', compact('variable'));
  }

//uploading images to DB
public function upload() {
    // getting all of the post data
    //$file = array('image' => Request::file('image'));
    $file = Input::file('images');

    //counting all selected images
    $file_count = count($file);
    $uploadcount = 0;

    // setting up rules
    foreach($file as $file) {
      $rules = array('form' => 'required');

      // doing the validation, passing post data, rules and the messages
      $validator = Validator::make(array ('form'=>$file), $rules);


      if ($validator->passes())
      {
        $destinationPath = 'path/to/upload/folder';
        $filename = $file->getClientOriginalName();
        $upload_success = $file->move($destinationPath, $filename);
        $uploadcount ++;

        $extension = $file->getClientOriginalExtension();
        $entry = new Model;
        $entry->format = $file->getClientMimeType();
        $entry->filename = $filename;
        $entry->save();
      }
    }

    if($uploadcount == $file_count)
    {
      Session::flash('message', 'Uploaded successfully');
    }
    else {
      return Redirect::to('your/view')->withInput()->withErrors($validator);
    }
  }

有谁可以提供帮助? 提前致谢

1 个答案:

答案 0 :(得分:0)

这足够接近我想要实现的目标。我添加了一个下载按钮,用于选择所选图像并下载。 这是重新构建的控制器

    Use DB;
class controller_name extends controller{
//index function goes here

//created a new function to download images for viewing 
public function download(){
$download=DB::table('table_name')->get();
return view('path.to.view',compact('download'));

我使用的路线就是这个

Route::get('download', 'Controller_Name@download');

在我的index.blade.php

这就是我改变结构

的方式
   <td>
     <a href="path/to/where/your/app/stores/uploaded/images{{$download->filename}}" 
download = "{{$download->filename}}">
<button type="button" class="btn btn-primary">View                                     </button>
</a>

有了这个,我现在可以下载并查看保存在数据库中的图像。

感谢https://www.youtube.com/watch?v=AlnackyPJPY