@webnerd's avatar

How can I come up with a functionality in vue.js that access files from a laravel 5.4 storage/app. I just need a functionality in vue.js that I can implement through a link.

I need to implement a file upload and download functionality in my laravel 5.4 and vue.js application. So far the file uploads are running smoothly but the problem is the file downloading. I am getting a notFoundHttpException when I try to click on the link instead of getting the downloads. I have created the following method in my DocumentController:

 public function returnDocument($id)
    {

        $document = Document::findOrFail($id);

        \Log::info($document);

        // abort unless Auth owns the doc ID
        // abort_unless(($document->user_id == Auth::user()->id || Auth::user()->role != 'tenant'), 403);

        if ($document->type == "contract") {
             return response()->download(storage_path('contracts/' . $document->location));
         }

        // serve the doc back as a download
        $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

        return response()->download($storagePath . "documents/" . $document->location);

     }

    ///**

The vue.js file is as follows:

 <div class="column">
                                <a v-bind:href="getDocumentDownloadLink(application.leaseholder_payslip.id)">
                                    Download
  </div>                              </a>

  methods: {

                        getDocumentDownloadLink: (id) => {

                return "/documents/" + id;
            },
0 likes
1 reply
Snapey's avatar

You should not be able to access anything in the storage folder directly from a client.

Only content in the public folder should be visible to the outside world.

You can make some Storage content visible by using the storage:Link artisan command.

See https://laravel.com/docs/5.5/filesystem#the-public-disk

Please or to participate in this conversation.