doudandui1592 2018-10-09 19:42
浏览 57
已采纳

为什么下载PDF的超链接不起作用? (没有文件)

I developed system for creating articles, each post except title and content consist possibility for uploading PDF.File names are stored in the database and they are written on the hyperlinks necessary to download the file.Now the problem is that when I want to download the file, it shows me a mistake in the browser. Did anyone knows what is a problem ?

Database

enter image description here

Location of PDF files

enter image description here

result

enter image description here

Posts Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
//including post model to controller
use App\Post;
//if we want to use sql syntax for queries 
use DB; 

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        //list articles by desc row

        $posts= Post::orderby('created_at', 'desc')->paginate(3);
        return view('posts.index')-> with('posts', $posts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        $this -> validate($request,[
            'title' => 'required',
            'content' => 'required'

        ]);


     // Handle File Upload
        if($request->hasFile('image')){
            // Get filename with the extension
            $filenameWithExt = $request->file('image')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('image')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('image')->storeAs('public/upload', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }









        //create new post
        $post= new Post;

        $post -> title = $request -> input('title');
        $post -> content = $request -> input('content');
        $post->file_name = $fileNameToStore;
        $post -> save();


            return redirect('/posts') ->with('success', 'Post Created');



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //list post while we enter through link
        $post= Post::find($id);
        return view('posts.show')-> with('post', $post);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
         $post= Post::find($id);
        return view('posts.edit')-> with('post', $post);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
            $this -> validate($request,[
            'title' => 'required',
            'content' => 'required'

        ]);
        //create new post
        $post= Post::find($id);
        $post -> title = $request -> input('title');
        $post -> content = $request -> input('content');
        $post -> save();
            return redirect('/posts') ->with('success', 'Post Updated');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $post= Post::find($id);
        $post -> delete();
          return redirect('/posts') ->with('success', 'Post Removed');
    }
}

show.blade.php

@extends('layouts.app')

@section('content')

    <h1>{{$post->title}}</h1>
    <div>
        {{$post->content}}

    </div>      
    <p><a href="./storage/app/public/upload/{{$post->file_name}}" download>Download the pdf</a></p>
    <small>Written on {{$post->created_at}}</small>
     <a href="/posts" class="btn btn-default">Return</a>

@endsection

Migration Create Post

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->mediumText('content');
            $table->string('file_name')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
  • 写回答

1条回答 默认 最新

  • dongxiezhuo8852 2018-10-09 19:49
    关注

    storage/app/public is not visible to the user

    You need to either move to public/... and/or create a symbolic link using this instructions: https://laravel.com/docs/5.7/filesystem#the-public-disk

    php artisan storage:link

    Then you replace

    <a href="./storage/app/public/upload/{{$post->file_name}}" download>
    

    With

    <a href="{{asset('storage/upload/'.$post->file_name)}}" download>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。