Jonjie's avatar
Level 12

Returns a 404 error when linking into a file

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

0 likes
25 replies
deansatch's avatar

what is the output of $exam->file_path?

1 like
deansatch's avatar

and does that path exist inside your public folder?

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

deansatch's avatar

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's avatar
Level 12

@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's avatar

Could you try without spaces in the filename?

1 like
deansatch's avatar

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's avatar

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

1 like
Jonjie's avatar
Level 12

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

Jonjie's avatar
Level 12

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

deansatch's avatar

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's avatar
Level 12

Indeed. I've also include my pdf files in public directory.

Jonjie's avatar
Level 12

Oh Sorry about that, that's exactly how it looks like in my setup.

deansatch's avatar

Sorry I posted the wrong url on the 2nd last line - I've updated it so that it is now correct.

So where does the 'school' directory come into this?

1 like
deansatch's avatar

Have you tried any of the things I have suggested? Have you tried loading the url without public in it?

What is the url that successfully loads your home page?

Jonjie's avatar
Level 12

Hi @deansatch . Yes, I tried to remove the public. But still getting the error. Anyway, I know the solution now :)

Jonjie's avatar
Jonjie
OP
Best Answer
Level 12

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.

Snapey's avatar

Thats what urlencode takes care of

>>> urlencode('Examination #1.pdf')
=> "Examination+%231.pdf"
1 like
deansatch's avatar

So when you said you tried all our suggestions like urlencode, renaming etc... you actually didn’t?

Please or to participate in this conversation.