Can't get blueimp / jQuery-File-Upload and ZF2

2019-06-10 20:55发布

问题:

Has anybody expierience with 'blueimp / jQuery-File-Upload' in combination with Zend Framework 2? I am struggeling to get is working.

The form seems to do his thing and works but when the controllers wants to do his magic, I am stuck.

This is my function that is called by the script to upload and save the files.

public function indexAction() 
{
    $request = $this->getRequest();

    $files =  $request->getFiles();
    $httpadapter = new \Zend\File\Transfer\Adapter\Http();

    if($httpadapter->isValid()) {

        $httpadapter->setDestination('data/images/uploads/');
        $httpadapter->receive($files);

        $return = 'valid';

    } else {
        // Form not valid, but file uploads might be valid...
        // Get the temporary file information to show the user in the view
        $return = $httpadapter->getMessages();
    }


    return new \Zend\View\Model\JsonModel(array($return));
}

The return I get is:

[{"fileUploadErrorFileNotFound":"File images1.jpeg was not found"}]

print_r($files) gives me this output:

Zend\Stdlib\Parameters Object
(
[storage:ArrayObject:private] => Array
    (
        [files] => Array
            (
                [0] => Array
                    (
                        [name] => images1.jpeg
                        [type] => image/jpeg
                        [tmp_name] => /private/var/tmp/phpa3IOwX
                        [error] => 0
                        [size] => 10185
                    )

            )

    )

)

Can anybody help me so I can upload files?

greetings,

回答1:

Your snippet works fine for me, it must be something else.

For instance, the adapter adds a validator \Zend\Validator\File\Upload. This is actually where your error comes from: Upload.php#L32, which is thrown here: Upload.php#L158. I am very curious what is happening in your situation, and why it throws the error. Could you do some prints from the validator?

It basically means that the files array in the validator is not the same array as the one you printed or the parameters passed to the validator are no good.

Also, which version of zf are you using?


On a sidenote you can reduce your code slightly, there's no need to pass the files array, as \Zend\File\Transfer\Adapter\Http converts the $_FILES itself.

$httpadapter = new \Zend\File\Transfer\Adapter\Http();

if($httpadapter->isValid()) {

    $httpadapter->setDestination('data/images/uploads/');
    $httpadapter->receive();

    $return = 'valid';

} else {
    // Form not valid, but file uploads might be valid...
    // Get the temporary file information to show the user in the view
    $return = $httpadapter->getMessages();
}

return new \Zend\View\Model\JsonModel(array($return));

You can also check a single file ( which blueimp should post ), by passing the field name as a parameter to isValid() and receive()

// ...
if($httpadapter->isValid('upload_name')) {
    // ...
    $httpadapter->receive('upload_name');
    // ...
}