Jump to content
JeevanisM

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

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

 

Share this post


Link to post
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.

Share this post


Link to post
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 ?

Share this post


Link to post
Share on other sites

I would suggest you to use the fantastic Tracy Debugger tool and try to find out whats going on. 

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?

Share this post


Link to post
Share on other sites

Hi @JeevanisM,

 

Try this:

echo $page->pagefile->httpUrl;

$page: the current page

pagefile: the page file field

httpUrl: the full url of the pagefile

The whole statement means echo the full url of the pagefile in current page.

Hope this helps.

Gideon

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hi @JeevanisM,

Let me see.

You should have code like this:

<a href="<?php echo $page->pagefile->httpUrl; ?>">Click here to download</a>

The code above should output a link to download the file.

Gideon

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...