Process Ajax file upload with Symfony2 - symfony

I used to have a ZF controller that was processing a fineuploader ajax upload. The code was simple:
$adapter = new Zend_File_Transfer_Adapter_Http();
$filename = uniqid();
$adapter->addFilter('Rename', APPLICATION_PATH . "/../public/temp-images/" . $filename);
$adapter->addValidator('Size', false, array("max" => "2MB"));
$adapter->addValidator('isImage', false);
if ($adapter->receive()) {
// Get mime type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, APPLICATION_PATH . "/../public/temp-images/" . $filename);
finfo_close($finfo);
preg_match('/(.*)\/(.*)/', $mimeType, $matches);
$extension = '.' . $matches[2];
Now I'm refactoring using Symfony2 and I have difficulties doing the same thing. This is what I have so far:
$form = $this->createFormBuilder()
->add('qqfile', 'file', array('constraints' => new File(array('maxSize' => '2M'))))
->getForm();
if ($form->isValid()) {
die('yes');
} else {
die('no');
}
This is what gets sent from the browser:
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qquuid"
b977c4b2-0edb-486b-aa86-4558275598aa
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqtotalfilesize"
14092
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqfile"; filename="ae35e28.png" Content-Type:
image/png
------WebKitFormBoundaryYPzt2RqJ6W4awSFp--
Now, I know for sure that the form will not be validated, because the POSTed data contains no name for the form. Actually, I don't even need to validate the whole form, just the uploaded file (like here Symfony2: upload a file using a file upload plugin), but how do I use validation for it?

I eventually figured it out myself.
Instead of using a form without a class, I created a class form whose getName() method returns an empty string. I set mapped=false for all the other fields except qqfile and also disabled the csrf protection for the form. In this way, the form gets properly submitted and the file input validated.

Related

Symfony Base64_encode on pdf in response

I want to base64_encode a pdf file before returning it to the client.
Here is what I do
$response = $event->getResponse();
$response->headers->remove('Content-Disposition');
$response->setContent(
$response->headers->get('Content-Type')
. ';base64,'
. base64_encode($response->getContent())
);
$response->headers->set('Content-Type', 'text/plain');
The pdf that I get in a browser when I put data:<base64_encoded_string> doesn't have any value, but the whole skeleton/css is ok.
If I do
$response = $event->getResponse();
$response->headers->remove('Content-Disposition');
$response->headers->set('Content-Type', 'application/pdf');
I do get a valid pdf file with all the values.
Is it possible that the base64_encoding is breaking something ?
Thanks
I found the answer, I had a sub-request, so its content was also base64 encoded, that's why the master request's content was broken.

how to set the download path in php

I'm a web developer.
DownloadController.php
$local_file = 'file.zip';
$download_file = 'd:\temp\download.zip';
if(file_exists($local_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($local_file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($local_file));
readfile($download_file);
}
I hope download the 'file.zip' and downloaded path that 'd:\temp\download.zip'
Anyone help me!
Thank u.
You can use following code to download a file:
return response()->download($pathToFile);
OR
return response()->download($pathToFile, $name, $headers)
The download method may be used to generate a response that forces the user's browser to download the file at the given path. The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:
Docs

HTTPful attach file and json-body in one request

I need to upload files via Rest and also send some configuration with it.
Here is my example code:
$this->login();
$files = array('file'=>'aTest1.jpg');
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$response = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files)
->body(json_encode($data))
->sendsJson()
->send();
I am able to send the file or able to send the body. But it is not working if I try with both...
Any Hint for me?
Regards
n00n
For those coming to this page via Google. Here's an approach that worked for me.
Don't use attach() and body() together. I found that one will clear out the other. Instead, just use the body() method. Use file_get_contents() to get binary data for your file, then base64_encode() that data and place it into the $data as a parameter.
It should work with JSON. The approach worked for me with application/x-www-form-urlencoded mime type, using $req->body(http_build_query($data));.
$this->login();
$filepath = 'aTest1.jpg';
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$req = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken());
if (!empty($filepath) && file_exists($filepath)) {
$filedata = file_get_contents($filepath);
$data['file'] = base64_encode($filedata);
}
$response = $req
->body(json_encode($data))
->sendsJson();
->send();
the body() method erases payload content, so after calling attach(), you must fill payload yourself :
$request = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files);
foreach ($parameters as $key => $value) {
$request->payload[$key] = $value;
}
$response = $request
->sendsJson();
->send();

Symfony2 PHPWord response

I am trying to generate a docx document on Symfony2, using the PHPWord bundle.
In my controller, I succeed in returning a docx file, but it is empty, I think it comes from my faulty response format.
public function indexAction($id)
{
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
return new Response($objWriter->save('helloWorld.docx'), 200, array('Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));
}
Try this class
<?php
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use Symfony\Component\HttpFoundation\Response;
class WordResponse extends Response
{
/**
* WordResponse constructor.
* #param string $name The name of the word file
* #param PhpWord $word
*/
public function __construct($name, &$word)
{
parent::__construct();
// Set default zip library.
if( !class_exists('ZipArchive')){
Settings::setZipClass(Settings::PCLZIP);
}
$writer = IOFactory::createWriter($word, 'Word2007');
//Set headers.
$this->headers->set("Content-Disposition", 'attachment; filename="' . $name . '"');
$this->headers->set("Content-Type", 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$this->headers->set("Content-Transfer-Encoding", 'binary');
$this->headers->set("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
$this->headers->set("Expires", '0');
$this->sendHeaders();
$writer->save('php://output');
}
}
Then in your controller do:
return new WordResponse($phpWord, "filename.docx");
Thanks a lot for your answer.
I achieve using the 2nd method, which is in my opinion the best.
I just have to return a response, otherwise the file was generated, but stuck in the web directory.
Using this response, everything was fine and a download prompt appeared, with the "full" file.
Here's my code :
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$filename="MyAwesomeFile.docx";
$objWriter->save($filename, 'Word2007', true);
$path = $this->get('kernel')->getRootDir(). "/../web/" . $filename;
$content = file_get_contents($path);
$response = new Response();
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename);
$response->setContent($content);
return $response;
PHPWord->save() returns a true value so that would be why your file is not being downloaded. With your return new Response() you are setting the content of your response to true (the result of your save call) which is why your response is empty.
You have 2 (and probably more that I haven't thought of) options to generate and download this file..
1. Save your file to a temp folder and server from there
$filename = sprintf(
'%s%sDoc-Storage%s%s.%s',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
uniqid(),
'docx'
);
$objWriter->save($filename);
$response = new BinaryFileResponse($filename);
For more info on the BinaryFileResponse see the docs.
2. Ignore Symfony and serve directly via the PHPWord action
$objWriter->save($filename, 'Word2007', true);
exit();
The ->save method provides all of the actions to download the generated file internally (see the code) so all you need to do is set the format and the third parameter to true and it will handle all of the headers for you. Granted it won't be returning a Symfony response but you will be exiting out before you get to that exception.

How to retrieve a streamed response (e.g. download a file) with Symfony test client

I am writing functional tests with Symfony2.
I have a controller that calls a getImage() function which streams an image file as follows:
public function getImage($filePath)
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'image/png');
$response->setCallback(function () use ($filePath) {
$bytes = #readfile(filePath);
if ($bytes === false || $bytes <= 0)
throw new NotFoundHttpException();
});
return $response;
}
In functional testing, I try to request the content with the Symfony test client as follows:
$client = static::createClient();
$client->request('GET', $url);
$content = $client->getResponse()->getContent();
The problem is that $content is empty, I guess because the response is generated as soon as the HTTP headers are received by the client, without waiting for a data stream to be delivered.
Is there a way to catch the content of the streamed response while still using $client->request() (or even some other function) to send the request to the server?
The return value of sendContent (rather than getContent) is the callback that you've set. getContent actually just returns false in Symfony2
Using sendContent you can enable the output buffer and assign the content to that for your tests, like so:
$client = static::createClient();
$client->request('GET', $url);
// Enable the output buffer
ob_start();
// Send the response to the output buffer
$client->getResponse()->sendContent();
// Get the contents of the output buffer
$content = ob_get_contents();
// Clean the output buffer and end it
ob_end_clean();
You can read more on the output buffer here
The API for StreamResponse is here
For me didn't work like that. Instead, I used ob_start() before making the request, and after the request i used $content = ob_get_clean() and made asserts on that content.
In test:
// Enable the output buffer
ob_start();
$this->client->request(
'GET',
'$url',
array(),
array(),
array('CONTENT_TYPE' => 'application/json')
);
// Get the output buffer and clean it
$content = ob_get_clean();
$this->assertEquals('my response content', $content);
Maybe this was because my response is a csv file.
In controller:
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
The current best answer used to work well for me for some time, but for some reason it isn't anymore. The response is parsed into a DOM crawler and the binary data is lost.
I could fix that by using the internal response. Here's the git patch of my changes[1]:
- ob_start();
$this->request('GET', $uri);
- $responseData = ob_get_clean();
+ $responseData = self::$client->getInternalResponse()->getContent();
I hope this can help someone.
[1]: you just need access to the client, which is a
Symfony\Bundle\FrameworkBundle\KernelBrowser

Resources