Cake PHP file Downloading using Media view - cakephp

Hi I uploaded files and stored in app/views/xxx/yyy.
Now i would like to download the files
If the file is PDF it is opened in new Tab and rest of them are downloaded, I need all files to be opened in new tab.
Here is my Code :
$this->view = 'Media';
$params = array(
'id' => $fileName,
'name' => $fileNameWOExtn,
'download' => false,
'extension' => $fileExtn, // must be lower case
'path' => APP . 'views' . DS .'static' . DS. $url .DS // don't forget terminal 'DS'
);
LogUtil::$logger->debug('Media View Params : '. var_export($params, true));
$this->set($params);

Not every file can be opened in the browser. A lot of file type encodings are proprietary and require their specific program to read and interpret them. PDFs, plain text files, some images, etc (with some exceptions) are the only types that browsers can recognize.
What types are files are you trying to render?

Related

TYPO3 TCA Overriding exists file

I have an issue like this with TYPO3.
I have an object, this object has file attribute, this field named "pdf"
In the TCA this field I defined like this:
'pdf' => array(
'exclude' => 1,
'label' => 'LLL:EXT:locations/Resources/Private/Language/locallang_db.xlf:tx_locations_domain_model_location.pdf',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'files', array(
'appearance' => array(
'createNewRelationLinkTitle' => 'LLL:EXT:cms/locallang_ttc.xlf:images.addFileReference',
),
'size' => 1,
'minitems' => 0,
'maxitems' => 1,
), $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
)
),
And now I can upload a file or image for this field, but there are some things does not good:
I want to allow PDF files only
After upload one file, if I upload another file, is said that "The existing files be overwritten" but old file never be overwritten.
The new one is also not uploaded.
What I need for this case: If I upload a new file, the old file will be overwritten by the new one.
Thanks for your help.
Since you pass in $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] you are currently allowing all kind of image-like file extensions. This is the point where you might want to use 'pdf' instead.
If you want to overwrite files, you'll need to to this in the File module of TYPO3. If you want to put a different file in your relation, you can remove the current one and add another one. The first file won't be deleted automatically but your record will have a relation to the new file.

Using CakePdf data not showing

I am in a problem with CakePDF. I am using CakePHP 2.3.6. I setup everything alright, bootstrap & core files are ok.
Now, I want to generate many pdf files and download them. Downloading is ok, but when I download the files, I see that the generated pdf files say that Undefined $data, where I send $data to the view file.
Here is my Controller code:
App::uses('CakePdf', 'CakePdf.pdf');
// ...
public function createPdf($id) {
$data = $this->DBTable->find('all', array(
'conditions' => array(
'DBTable.id' => $id
)
));
$CakePdf = new CakePdf();
$path = APP . DS . 'pdf' . DS;
foreach($data as $key => $var) {
$CakePdf->template('create', 'default');
$this->set('var', $var);
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => $var['id'] . ".pdf"
);
$CakePdf->write($path . $var['id'] . ".pdf");
}
}
Here, I am downloading the files some other way, not related to this problem, because downloading is ok. When I run this, all files are downloaded, and when I see the files, they say "Udefined var", looks like they didn't receive and variable called "$var".
I put the view file & the layout exactly where ceeram said.
What should I do now ? Please help me.
At last I got a solution to this. In the Controller function :
$data=$this->ModelName->find('all');
$CakePdf=new CakePdf();
$CakePdf->template('view_file','default');
$this->pdfConfig=array('orientation'=>'portrait','filename'=>$data['name']);
$CakePdf->viewVars(array('data'=>$data));
$CakePdf->write($path.$data['name'].".pdf");
Here, we have to store view_file.ctp in View/Pdf/, and default in View/Layouts/pdf/, where view_file.ctp is the view file which we want to convert in pdf, and default is the layout for this specific view file.
Here, viewVars() function passes the data to the view file, just like :
$this->set->('data',$data);
Let me know if anything is not clear here.
Thanks.

Wrong File Type for File Download - CakePHP

In my cakephp web page, I have a button that the user clicks and it performs some maintenance on the server and then presents a download to the user. The file download is testlogs.gzip but when the download option occurs in firefox, it comes up as a file type of html document.
The file downloads and extracts fine, but I want the file type to be correct.
Here is the Media view class for the download:
//$this->autoRender = false;
$this->viewClass = 'Media';
$params = array(
'id' => 'systemlogs',
'name' => 'testsystemlogs',
'download' => true,
'extension' => 'gzip',
// 'mimeType' => 'zip',
'path' => DS . 'home' . DS
);
$this->set($params);
I tried to add in the 'mimeType' => 'zip' but that did not work.
Any help would be great.
Thanks
UPDATE: I tested uploading of the same files and I used firebug to determine the content type. The only file types I care about are of type: application/octet-stream.
So I think I just need to set this type in the Media class settings, not sure how to do that though.
THanks
mimeType has to be specified as an associative array (extension -> type), in your case this would be array('gzip' => 'application/x-gzip'). CakePHP merges this array with the built-in array of known MIME types in CakeResponse class.
However, CakePHP already knows the MIME type of .gz files so changing the extension of your file, if it is an option, might be an even easier solution.

Get selected file from media module

I am trying to get the file that the user has selected from the media selector from the media module in Drupal 7.
My form contains the selector and can upload and select a file successfully, but unable to get the name of the file that has been chosen.
My form for the selector:
$form['file'] = array(
'#type' => 'media',
'#title' => t('Screenshot'),
'#description' => t('Upload an image of the feature (Optional)'),
),
);
I need to get the details of the selected file (e.g. name, directory)
Try to use hook_submit to check the file name.
Suposing the file field in your form is named "file":
function your_form_submit($form, &$form_state) {
$file=$form_state['values']['file'];
// do something...
file_save($file);
You can use form_vaidate as well, i don't know exactly what you want to do with the file name...

How to use MeioUpload Behavior for uploading any kind of files not only images

I would like to use the MeioUpload Behavior for uploading any kind of documents(I want every extension to be accepted). I've already seen this question , but it didn't work for me, for some strange reason I can only upload image and pdf files for the other types of files I get this error when I attempt to submit the form : "The post could not be saved. Please, try again."
Edit: Well, it looks like I was finally able to upload other kind of files apart from images,I had to write the options 'allowedMime' and 'allowedExt' in camelCase (in the documentation they use the underscore version 'allowed_mime', 'allowed_ext' I don't know why:( ), but I haven't been able to upload .zip files and most importantly I still don't know how to tell the behaviour to accept anything
var $actsAs = array(
'MeioUpload' => array(
'link_referencia' => array(
'dir' => 'files{DS}uploads',
'create_directory' => true,
'allowedMime' => array('application/pdf', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel', 'application/rtf', 'application/zip'),
'allowedExt' => array('.pdf', '.doc', '.ppt', '.xls', '.rtf', '.zip'),
'default' => false,
)
)
);
Thanks in advance
It can be use for anything, hence it's called meioUpload and not meioImage
Just set the allowed file extensions and mime types when you initialiave the behavior.
Step 4 has an example http://www.meiocodigo.com/projects/meioupload/

Resources