Reputation: 175
I'm facing two problems.
when I try to download a txt extension file the functions work fine below code.
public function index()
{
$data = "some text";
$name = "sample.txt";
force_download($name,$data);
}
but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.
public function index()
{
$data = "some text for pdf";
$name = "sample.pdf";
force_download($name,$data);
}
Upvotes: 0
Views: 1916
Reputation: 441
pass name of the file you want to download
Controller
function download($file_name)
{
$this->load->helper('download');
force_download('./assets/'.$file_name, NULL);
}
View
<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>
For second question try onclick()
Upvotes: 0
Reputation: 7997
You cannot create a (not currupt) pdf file with force_download()
. You'll need to create the pdf file using a php library like fpdf or similar. The force_download()
function only generates a header which forces a download to happen.
If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);
concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box
Upvotes: 3