Returns a 404 error when linking into a file

Published 4 months ago by jonjie

Hi guys, I'm trying to visit a specific pdf file in my laravel app. but it returns an error that's telling me to create a route. I think it's because of the path I'm giving. Do you have any idea to solve this? Please see my code below for your reference:

<iframe src="{{ asset($exam->file_path) }}" frameborder="0"></iframe>

I'm basically returning $exam from my controller.

If I dd() the $exam->file_path, it returns file/1/pdf/Examination part 1.pdf

Best Answer (As Selected By jonjie)
jonjie

My pdf file contains hash sign (#) that treats the file as id not the exact file name. Example, if you have a file name like Examination #1.pdf, just remove the # sign so it will not be treated as an ID in the url.

deansatch

what is the output of $exam->file_path?

jonjie

Hi @deansatch , thank you for quick response. Please see the update. Thanks

deansatch

and does that path exist inside your public folder?

public/file/1/pdf/Examination part 1.pdf

jonjie

@deansatch Yes. I've already created the path, and put the file in it.

deansatch

do you have a route already set up similar for a page e.g.

'file/{id}/pdf/{pdf_id}'

I would comment out most or all of your routes and if it works start uncommenting them one by one

jonjie

@deansatch No I haven't. I just want to access the exact pdf file without any route. Because sometimes I'm having a problem when viewing pdf file if I use <object> or <iframe>. Sometimes it returns a blank page, that's why I'm decided to use the exact file instead.

Procat

Could you try without spaces in the filename?

deansatch

Try creating a route for your pdf...something like /pdf and then in that closure or referenced controller use:


<iframe src="{{ route('pdf') }}" frameborder="0"></iframe>



Route::get('pdf', function(){

      return response()->file(public_path('file/1/pdf/Examination part 1.pdf') );

});


Snapey
Snapey
4 months ago (894,315 XP)

try url encoding the filename so that you are not getting spaces and other invalid characters in there

jonjie

Hi @Snapey . How can I do that? Can you give me an example? thanks.

deansatch

urlencode($exam->file_path)

jonjie

URL Encoding is not working. If I try to paste the result in the url bar, It returns a 404 error.

deansatch

have you tried renaming the file so it has no spaces? Ensured the path is correct? In the address bar it would not include '/public/'. What does the full url look like?

jonjie

Yes I've already tried to rename it without spaces. '/public' folder is needed here because all of my pdf files are stored there. The full url looks like this: http://localhost/school/public/files/1/PDF/exams/fillable/ExaminationPart1.pdf

deansatch

Remove public from the url and try again. If this is a laravel project public would be your public root i.e. if http://localhost/ loads your laravel home page then it is actually pointing to your public folder.

https://laravel.com/docs/5.5/structure#the-public-directory

Please sign in or create an account to participate in this conversation.