Open attachments in a new browser window

Hi,

If you click an attachment in SuiteCRM, for example in the Notes section, then the file will be downloaded.

We have a lot of attached PDF’s in our CRM. Is it possible to open/view the PDF (in a new browser window) instead of having to download the file each time?

Anyone?

I think it should be possible, but can it be done in an upgrade safe way?

When requesting an attachment in SuiteCRM the response will include the following headers:

Content-Disposition: Attachment
Content-Type: application/octet-stream

This will result in the browser downloading the file.

In order to have the browser open the attachment these headers should be changed into:

Content-Disposition: Inline
Content-Type: application/pdf

It seems that download.php is the place to start although changing this file directly won’t be upgrade safe.

For testing purposes I tried to output some content directly in this file using either

$GLOBALS[‘log’]->debug(“My debug message”);

or

error_log(“Output some message”);

However, no output is generated.

Is there a way of outputting variables in download.php for debugging purposes?

Turns out that the output statements in download.php do not work if you test SuiteCRM using localhost. In fact, the file appears to be not used at all. I removed it and the crm kept working without errors.

Change localhost to your local IP address instead and debugging works just fine. Also, in this case, removing download.php will result in a fatal error which seems to be more logical.

OK I implemented it by changing download.php which is not upgrade safe. I will try to come up with an upgrade safe solution later.

In download.php change the following code:

if (isset($row['file_mime_type']) && strpos($row['file_mime_type'], 'image/') === 0) {
    $mime_type = $row['file_mime_type'];
}

into

if (isset($row['file_mime_type']) && 
	(strpos($row['file_mime_type'], 'image/') === 0 || $row['file_mime_type'] == "application/pdf")) {
    $mime_type = $row['file_mime_type'];
}

Also change the following line of code:

header("Content-Disposition: attachment; filename=\"".$name."\";");

into

if ($mime_type == "application/pdf" || (strpos($mime_type, 'image/') === 0)) {
    header("Content-Disposition: inline");
} else {
    header("Content-Disposition: attachment; filename=\"".$name."\";");
}

These changes will make sure attached PDF’s and images will be directly viewed in your browser.

If anyone has input for an upgrade safe solution then please share your ideas.

2 Likes

I finally made my changes upgrade safe by adding a custom view button to the Notes subpanel (which I separated from the History subpanel).

Hi Luolun!

I would appreciate if you could share your solution for custom view button for files in Notes / Documents modules.

It is a nightmare to download the file each time you’d like to check it out…

Thx a lot in advance!

BR,

Mario