本文介绍了无法获得blueimp / jQuery-File-Upload和ZF2的运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人用blueimp / jQuery-File-Upload结合Zend Framework 2?我正在努力工作。



这个表格似乎在做他自己的事情,但是当控制者想要做他的魔法时,我被卡住了。

这是我的脚本调用的上传和保存文件的函数。

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

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

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

$ return ='valid';

} else {
//表单无效,但文件上传可能是有效的...
//获取临时文件信息以在视图中显示用户
$ return = $ httpadapter-> getMessages();
}


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

$ / code>

我得到的回报是:

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



print_r($ files)给了我这个输出:

  Zend \Stdlib\参数对象

[storage:ArrayObject:private] => Array

[files] => Array

[0] => Array

[name] => images1.jpeg
[type] => image / jpeg
[tmp_name] => / private / var / tmp / phpa3IOwX
[error] => 0
[size] => 10185











$ b

任何人都可以帮我上传文件吗?



问候,

解决方案

例如,适配器添加一个验证器 \ Zend\Validator\File\Upload 。这实际上是你的错误来自:,在这里引发:。我很好奇你的情况正在发生什么,为什么它会抛出错误。你能做一些来自验证器的打印吗?



它基本上意味着验证器中的文件数组与您打印的或者传递给验证器的参数都不行。



另外,您正在使用哪个 zf版本




在旁注中,您可以稍微减少代码,不需要传递文件数组,如 \Zend\File\Transfer\Adapter\Http 转换$ _FILES自身。

  $ httpadapter = new \Zend\File\Transfer\Adapter\Http(); 
$ b $ if($ httpadapter-> isValid()){

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

$ return ='valid';

} else {
//表单无效,但文件上传可能是有效的...
//获取临时文件信息以在视图中显示用户
$ return = $ httpadapter-> getMessages();
}

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

您也可以通过传递字段名称来检查单个文件(哪个blueimp应该发布)参数到 isValid() receive()



<$ p ($ httpadapter-> isValid('upload_name')){
// ...
$ httpadapter-> $ b $ // ...
if ;接收( 'upload_name');
// ...
}


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,

解决方案

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');
    // ...
}

这篇关于无法获得blueimp / jQuery-File-Upload和ZF2的运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:27