本文介绍了cakephp-2.4:应用jshelper ajax提交后,图像目录未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不使用ajax的情况下,cakephp图像上传工作正常。使用ajax之后,插入的数据没有图像目录。

Here cakephp image upload working fine without ajax.After using ajax, data is inserted without image directory.

这是我的模型代码

public function processCoverUpload($check = array()) {
            if (!is_uploaded_file($check['product_image']['tmp_name'])) {
                return FALSE;
            }
            if (!move_uploaded_file($check['product_image']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['product_image']['name'])) {
                return FALSE;
            }
            $this->data[$this->alias]['product_image'] = 'uploads/'. $check['product_image']['name'];
            return TRUE;
        }

此处是控制器

if ($this->request->is('post')) {
            $this->MadProduct->create();

            $data = $this->request->data['MadProduct'];
                        if (!$data['product_image']['name']) {
                            unset($data['product_image']);
                        }
            if ($this->MadProduct->save($data)) {
                $this->Session->setFlash(__('The mad product has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The mad product could not be saved. Please, try again.'));
            }
        }

这是视图文件字段

echo $this->Form->input('product_image',array('type' => 'file','label' => false,));

这是js提交按钮

 <?php  echo $this->Js->submit('Submit',array(
        'before'=>$this->Js->get('#sending')->effect('fadeIn',array('speed' => 'slow')),
        'success'=>$this->Js->get('#sending')->effect('fadeOut',array('speed' => 'slow')),
         'class' => 'btn btn-danger',
         'style'=>'width:45%;margin-top:1%;height:30px;')
         );
  ?>

我将如何通过jshelper发送图像目录?

How I will send image directory by jshelper ?

推荐答案

JS帮助程序不支持文件上传,这需要使用 new 功能。

The JS helper doesn't support file uploads, which would require making use of the "new" XHR2 features.

您可以扩展JS helper / engine并添加对文件的支持,但是由于JS helper是,我建议直接使用JavaScript。有足够多的示例显示如何通过AJAX上传文件。

You could extend the JS helper/engine and add support for files, but since the JS helper is deprecated anyways, I'd suggest to simply make use of JavaScript directly. There are more than enough examples out there showing how to upload files via AJAX.

How can I upload files asynchronously?

这篇关于cakephp-2.4:应用jshelper ajax提交后,图像目录未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:41