0

好吧,我有一个 CakePHP 2.2.5 应用程序,它基本上是用户管理,一个用户有一张照片,我创建了一个 PhotosController(作为本教程的建议)。我对其进行了测试,它可以完美地工作Photos/View,包括可以在不重定向的情况下以表单形式发送照片的 ajax 元素。现在我正在尝试将所有这些联系起来,但这并不像我想的那么容易。

我的照片添加方法:

public function add()
{
    if ( $this->request->is( 'post' ) ) {           
        $this->Photo->create();
        $json = array( 'result' => array( 'files' => array() ) );
        if ( $this->Photo->save( $this->request->data ) ) {
            $photo                             = $this->Photo->findById( $this->Photo->id );
            $json['files'][0]['name']          = $photo['Photo']['basename'];
            $json['files'][0]['size']          = $this->request->data['Photo']['file']['size'];
            $json['files'][0]['url']           = $this->webroot . 'media/transfer/img/' . $photo['Photo']['basename'];
            $json['files'][0]['thumbnail_url'] = $this->webroot . 'media/filter/thumbnail/img/' . $photo['Photo']['basename'];
        }
        else {
            $json = 'Error';
            $this->Session->setFlash( __( 'The photo could not be saved. Please, try again.' ) );
        }
        $this->RequestHandler->renderAs( $this , 'ajax' );
        Configure::write( 'debug' , 0 );
        $this->set( 'json' , json_encode( $json ) );
        $this->render( '/Elements/ajax' );
    }

我的测试视图(在用户控制器上):

<?php echo $this->Form->create(
        'Photo' ,
        array( 'url'     => array( 'controller' => 'photos' ,
                                   'action'     => 'add'
        ) ,
               'id'      => 'fileupload' ,
               'enctype' => 'multipart/form-data'
        )
    ); ?>
<-- Some HTML HERE-->
<?php
                echo $this->TB->input(
                    'Photo.file' ,
                    array(
                        'prepend' => 'Upload' ,
                        'type'    => 'file' ,
                        'class'   => 'fileUpload' ,
                        //'multiple' => 'multiple',
                        'div'     => FALSE ,
                        'between' => FALSE ,
                        'after'   => FALSE ,
                        'label'   => FALSE ,
                        'help'    => NULL ,
                    )
                );
                ?>
</form>
<-- Some (already tested) Javascript to make this work HERE-->

视图呈现时没有 php 错误,但是当我上传文件时出现 Javascript 错误,并且调试显示:

GET http://<server>/<app>/users/add 404 (Not Found) jquery.min.js:1960
  send jquery.min.js:1960
  b.extend.ajax jquery.min.js:1840
  (anonymous function) main.js:59
  c jquery.min.js:215
  p.fireWith jquery.min.js:249
  b.extend.ready jquery.min.js:69
  H jquery.min.js:10

实际上,我在 GET 和 POST 方法(当然有一些不同的 jQuery 行)上都遇到了这个错误。

甚至将操作设置为它试图访问用户的 PhotosController。我认为是因为视图,因为我没有在任何地方指定用户。我试图解决这个问题一个星期,但我没有更多的想法。任何帮助将不胜感激。

提前致谢。

4

0 回答 0