This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I am new to laravel and october cms. I want to add a button to allow users download a PDF file, 'a.pdf', without directing to a new page. I know that I have to use AJAX and send a download http response in order to make browsers show a 'Save As...' dialog box. So far this is what I have accomplished:
title = "Sandbox"
url = "/test"
layout = "default"
==
<?php
function onDownload()
{
$pathToFile = Url::to("/storage/app/media/a.pdf");
$fileName = "download.pdf";
$headers = [
'HTTP/1.1 200 OK',
'Pragma: public',
'Content-Type: application/pdf'
];
return Response::download($pathToFile, $fileName, $headers);
}
?>
==
<div class="container">
<form class="form-inline" data-request="onDownload">
<button type="submit" class="btn btn btn-primary" data-attach-loading>Download</button>
</form>
</div>
I get the following error using above code: "The file "http://localhost/october/storage/app/media/a.pdf" does not exist" on line 37 of E:\xampp\htdocs\october\vendor\symfony\http-foundation\File\File.php
But when I type the URL in browser download dialog appears just right.
What am I doing wrong?
You need to provide the path to the actual file as it relates to your local file system, not as a URL.
Instead of: $pathToFile = Url::to("/storage/app/media/a.pdf");
Use: $pathToFile = base_path('storage/app/media/a.pdf');
ndcisiv said:
You need to provide the path to the actual file as it relates to your local file system, not as a URL.
Instead of: $pathToFile = Url::to("/storage/app/media/a.pdf");
Use: $pathToFile = base_path('storage/app/media/a.pdf');
Thank you @ndcisiv. Your solution works fine for me.
1-3 of 3