Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning user uploaded file using custom controller #1029

Open
chattago2002 opened this issue Apr 2, 2024 · 2 comments
Open

Returning user uploaded file using custom controller #1029

chattago2002 opened this issue Apr 2, 2024 · 2 comments
Assignees

Comments

@chattago2002
Copy link

Hi everybody.

I made a custom controller to handle file uploads and I'd like to add the capability to return the file via API made using this repository.
In the function __construct function I added:

$router->register('GET', '/records/file/*', array($this, 'getFileV2'));

In

public function getFileV2(ServerRequestInterface $request): ResponseInterface

I need to get the file, decrypt (because in POST controller function the file is correctly uploaded as encrypted) but also if I add

header("Content-Type: image/png");
return $decryptedFile;

I can't view the file and the error message is:

UploadUserFile::getFileV2(): Return value must be of type Psr\\Http\\Message\\ResponseInterface, string returned

How can I do this?

@mevdschee
Copy link
Owner

mevdschee commented Apr 2, 2024

Good question, you shouldn't return a string. Try this:

return ResponseFactory::from(200, "image/png", $decryptedFile);

You can see some response fiddling in the XmlMiddleware, see:

https://github.com/mevdschee/php-crud-api/blob/main/src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php

You can read more about PSR7 in de dependency:

https://github.com/Nyholm/psr7

@mevdschee mevdschee self-assigned this Apr 2, 2024
@chattago2002
Copy link
Author

Good question, you shouldn't return a string. Try this:

return ResponseFactory::from(200, "image/png", $decryptedFile);

You can see some response fiddling in the XmlMiddleware, see:

https://github.com/mevdschee/php-crud-api/blob/main/src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php

You can read more about PSR7 in de dependency:

https://github.com/Nyholm/psr7

Thanks for reply. I'll try in a few days and then let know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants