How to correctly open files of any extension using Document (File name) type?

Hi everybody. I’m using MySQL database and SC v.7.1. I’ve created a form to register basic information of training events. All details of each event are in a PDF document, so I need to vinculate the basic information to it. In MySQL, I defined a TEXT field and used it in SC form as DOCUMENT (FILE NAME) type. In running mode, it works fine, so the PDF file is recorded in /…/wwwroot/scriptcase/file/doc.

The problem is when I tryied to view the PDF document in the application (running mode). It opens a blank session in my chrome browser and download a php file. The file has to be opened in PDF to view its contents but for normal users this procedure will be very complicated.

Thanks for any help!

Things because you don’t have correctly configured the paths AFAIR

You can add an PDF extension to chrome maybe you system PDF don’t allow/open.Or the file path couldn’t be clearly defined.

hi adeleon2, have a look here for the paths

http://www.scriptcase.net/forum/showthread.php?6854-sending-email-with-image-attachment-driving-me-crazy

How are you opening the file?

I have a button on each row of a grid labelled “view PDF”. clicking that button executes:

$tmp_fp = [fp] . $valueg;

sc_redir($tmp_fp, "", "_blank");

Using the sc_redir macro opens the PDF in the browser, as opposed to downloading it.

[fp] is the filepath so assign that with “…/wwwroot/scriptcase/file/doc”, and $value is the filename - so assign the filename to that variable.

guys, pdf should have the plugin enabled in the browser in order to open it directly
also you can define if sc opens it for your directly or ask the user to click download manually, you could try that out

also you can define if sc opens it for your directly or ask the user to click download manually, you could try that out

Where in SC is that done?

export > pdf confirguration

Ah ok - crossed purposes I think. My system has scanned PDFs uploaded - they are not generated by SC - so I just needed to figure out how to open them when clicked on in an app (as opposed to downloading them). So I think my original comment about using sc_redir() still stands.

You are right that is stands. The generated code is fault as I have reported before.
Example in SC7 for a download in my grid_filestorage.php of the generated code:


   if (is_file($trab_doc))  
   { 
       header("Pragma: public", true);
       header("Content-type: application/force-download");



My corrections:


   if (is_file($trab_doc))  
   { 
//if not applewebkit  so if ie/opera/.. and alike
if (isset($_SERVER['HTTP_USER_AGENT']) && (false == strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'applewebkit'))){
  header("Content-Description: File Transfer");
  header("Expires: 0");
  header("Pragma: public");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

and further on:


       else
       {
           header("Content-Disposition: attachment; filename=\"" . $nm_nome_doc . "\"");
       }
       readfile($trab_doc);
   } 

becomes:


  else
  {
         header("Content-Disposition: attachment; filename=\"" . $sProtectedFilename . "\"");
  }
  header('Content-Transfer-Encoding: binary');
  header("Content-Length: ".filesize($trab_doc));
}else{
   $finfo=finfo_open(FILEINFO_MIME_TYPE);
   if (strtolower(substr($filename,-4))=='.eml'){
     echo header("Content-type: message/rfc822");
   }else{
     echo header("Content-type: ".finfo_file($finfo,$trab_doc));
   }
   finfo_close($finfo);
   $sProtectedFilename = str_replace(array(' ', "'", '!', ',', '-', '+'), array('__SC_SPACE__', '__SC_QUOTES__', '__SC_EXCLAMATION__', '__SC_COMMA__', '__SC_MINUS__', '__SC_PLUS__'), $nm_nome_doc);
   $sProtectedFilename = urlencode($sProtectedFilename);
   $sProtectedFilename = str_replace(array('__SC_SPACE__', '__SC_QUOTES__', '__SC_EXCLAMATION__', '__SC_COMMA__', '__SC_MINUS__', '__SC_PLUS__'), array(' ', "'", '!', ',', '-', '+'), $sProtectedFilename);
   if (isset($_SERVER['HTTP_USER_AGENT']) && false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'chrome'))
   {
       header("Content-Disposition: attachment; filename=\"" . $sProtectedFilename . "\"");
   }
   elseif (isset($_SERVER['HTTP_USER_AGENT']) && false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'applewebkit'))
   {
       header("Content-Disposition: attachment; filename=\"" . $nm_nome_doc . "\"");
   }
   elseif (function_exists('NM_utf8_urldecode') && $nm_nome_doc != NM_utf8_urldecode($nm_nome_doc))
   {
       header("Content-Disposition: attachment; filename=\"" . $nm_nome_doc . "\" filename*=UTF-8''" . $sProtectedFilename);
   }
   else
   {
       header("Content-Disposition: attachment; filename=\"" . $sProtectedFilename . "\"");
   }
}
       readfile($trab_doc);
   } 

They did fix some of the bugs I detected but some are still left. The point is that if header(“Content-type: application/force-download”); is used that the browser undestands that this file must be downloaded (as chrome always does). If it is to be opened then you have to send the mime filetype, so this line then is important: $finfo=finfo_open(FILEINFO_MIME_TYPE);

Normally you would expect apache to properly handle the file types, but sadly enough it seriously screws up docx and xslx files and acts asif they are zip files (which they are also).

In scriptcase 7 there is no option to set the the default download behaviour tho so in order to get that working properly you have to seriously hack into the generated code… :frowning:

This same bad implementation (in my view) still exists in sc8.

Thanks rr - very detailed and helpful!

Thx for all these details.
A very close related question: using a “Document (File Name)” field in a form, I can upload a file it is saved correctly in the “doc” directory.
When I click later on the link, it opens a "Save As " dialog.
Is there a way to “send” the document to the browser when clicking the link instead of opening a “Save As” dialog?

1 Like