Jump to content

How to get a File Download link in a Page : PDF file attached


JeevanisM
 Share

Recommended Posts

Hello,

Please help me on the below issue. I am creating a school text book website, simply displaying the PDF files of books to download by the visitor. Here I have created a new single page for a chapter and added a PDF file to that page from Admin Dashboard. But in the front I am not able to get the File Download url link to that PDF using API / Page references. Steps I followed as below :

1. created a new fields item as PDFFILE

2. added this filed into the  Page template for Book

3. uploaded the PDF file to that page in Admin page edit

4. Saved and tried to use the API reference to get the full URL to the PDF file.

 

I used this  below api to get the download PDF url but this code is not working.  when I load the page in browser, it keeps loading and loading and on.....

$string = $pagefile->httpUrl();

 

Is there any other options available ? I simply a need a download PDF button to the attached PDF to that page itself


thank you

Jeevan S

 

Link to comment
Share on other sites

Hi @JeevanisM,

did you declare $pagefile before the call of "httpUrl()"? Maybe there are even log entries which give more information. 


In general it depends on wether you set a specific format for that specific field (single element or array) how to get the URL of the file. 

Assuming that your file field is named "pdffile":

Case 1 - Single element

$pdfUrl = $page->PDFFILE->httpUrl();

Case 2 - Array

You either have to loop through all files or e.g. to get the first file: 

$pdfUrl = $page->PDFFILE->first()->httpUrl();

You can check format settings on details tab of your field.

Link to comment
Share on other sites

23 hours ago, androbey said:

did you declare $pagefile before the call of "httpUrl()"? Maybe there are even log entries which give more information. 

How do I do that ??

 

I just added your code

$pdfUrl = $page->PDFFILE->httpUrl();

but the page is just loading and loading and keep loading.  So how should I declare the $pagefile ?

Link to comment
Share on other sites

2 hours ago, androbey said:

Maybe there is something else wrong. Does the page load normally if you comment those lines out where you are trying to get file URL?

yes the page loads fine when I remove the code. I even updated the core to latest dev from the standard, thinking if thats an issue

Link to comment
Share on other sites

2 hours ago, Gideon So said:

echo $page->pagefile->httpUrl; 

this doesnt return any values, meaning displays nothing. The page loads fine but no result for the code. I made sure the PDF file is attached in the page

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...