1

I have the following code which create hyperlinks:

<div class="col-md-12">
    {% set href = 'customers/outputfile/' ~ doc.ICDU_GUID %}
    <a href="{{ href | _base_url }}" target="_blank">{{ docName }}</a>
</div>

An example of a generated URL is localhost:8080/customers/outputfile/b41b055e-91ad-4dc8-8184-9cb910cb3e02

In the controller, I fetch the location of the document and then return the response:

public function outputFile($id) {
    $dbCustomersModel = new DB_CustomersModel();
    $filePath = $dbCustomersModel->getFilepathFromId($id);

    return $this->response->download($filePath->ICDU_DocLocation, null);
}

When I click on the hyperlink, a new tab is opened but closed immediately and the file download popup appears.

What I want is for the file to open in a new tab rather than being asked to download it. The files can be images, pdf, etc...

Jolan
  • 681
  • 12
  • 39
  • 1
    check this: https://stackoverflow.com/questions/14952514/how-to-force-open-links-in-chrome-not-download-them – Vickel Nov 03 '20 at 13:30

1 Answers1

0

By using

return $this->response->download($filePath->ICDU_DocLocation, null);

You are forcing the browser to download the file instead of trying to read it directly. To increase chances the file will be viewed instead of downloaded add headers

Content-Type: application/pdf - or other appropriate type Content-Disposition: inline;filename="myfile.pdf" - for attempting to call the browser to open it

Codeigniter 4 with response:

$response->setHeader('Content-Type', 'application/pdf')
         ->setHeader('Content-Disposition', 'inline;filename="myfile.pdf');

source: Opening files in browser instead of downloading

Tsefo
  • 409
  • 4
  • 15