getFiles() is always empty, no files uploaded

AndiLeni
Member | 3
+
0
-

Hello,

I tried to create an upload form with either uppy of a simple <input type="file"> tag.

Unfortunately, the method getFiles() is always empty, which assumes that my files were not uploaded at all.

Is there somewhere an example on how to correctly manage file uploads?

This is the Presenter which should handle the file upload: (the method actionUpload() gets triggered correctly when I submit my form)

namespace App\Presenters;

use Nette;
use Nette\Http\Request;

class UploadPresenter extends Nette\Application\UI\Presenter
{


    public function actionUpload()
    {
        $httpRequest = $this->getHttpRequest();

        $files = $httpRequest->getFiles();

        $this->sendJson($files);
    }
}

Thanks and kind regards
Andreas

Rick Strafy
Nette Blogger | 21
+
0
-

Hi, try to use onSuccess callback for handling forms, here is an example
https://github.com/…resenter.php#L68

dakur
Member | 301
+
0
-

@AndiLeni As @RickStrafy pointed out, the correct way of doing it is to use Nette Forms as it can be easily integrated into Nette presenters – see examples.

However, it should still work in this low-level way as uploaded files are part of HTTP request. Are you sure you added enctype="multipart/form-data" to <form> tag?

Last edited by dakur (2021-07-13 15:19)

Rick Strafy
Nette Blogger | 21
+
0
-

I would be surprised if that worked, because it looks like there is no valid nette form, and if nette app would accepted file just from some random form, it would mean that someone can edit html, add form for file uploading and use it for attacking purposes or just to overload server network.

dakur
Member | 301
+
+2
-

It's not accepted from random form but from $_FILES superglobal variable if I'm not wrong. The binding with a form is not responsibility of Nette's Http Request, but of Forms actually. So on a low-level you can reach uploaded files from request. It's up to you not to save it to storage if it's malicious.

Rick Strafy
Nette Blogger | 21
+
0
-

Yea, that makes sense, btw. @AndiLeni you don't have name="something" in your input, that's the problem.

AndiLeni
Member | 3
+
0
-

Thank you very much!
It was indeed a missing name="" attribute on the form.
Uppy does include one to render the document, but does not send a name by default when submitting the files.

Last edited by AndiLeni (2021-07-15 15:07)