首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP -表单未提交文件字段

CakePHP -表单未提交文件字段
EN

Stack Overflow用户
提问于 2011-08-23 06:36:27
回答 2查看 1.9K关注 0票数 0

我正在尝试将图像文件上传到我的应用程序,用户可以选择通过上传图像来更改其个人资料图片,但当表单提交时,它不会将文件与表单一起提交。

我正在使用Meio Upload上传我的文件,我将粘贴我的视图控制器动作和模型:

查看:

代码语言:javascript
复制
<div class="prepend-1 span-8">
<?php
    echo $this->Form->create('Fonyker', array('action' => 'edit', 'class' => 'span-8', 'type' => 'file'));
?>
    <div class="span-3">
      <label for="FonykerImage" class="span-3 last">Profile Image</label>
      <div class="span-4 preview" style="border-color:black; border:dotted 2px; height:80px; width:80px">
        <img src="<?php echo $this->data['Fonyker']['image_url'];?>" style="width:80px;height:80px;"/>
      </div>
    </div>
    </br>
<?php
    echo $this->Form->input('image_url', array(
      'div' => array(
        'class' => 'span-5 last'
      ),
      'label' => array(
        'text' => 'Upload an image'
      ),
      'type' => 'file'
    ));

    echo $this->Form->input('username', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Username'
      ),
      'onsubmit' => 'return false;'
    ));

    echo $this->Form->input('email', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Email'
      ),
      'onsubmit' => 'return false;'
    ));

    echo $this->Form->input('name', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Name'
      ),
      'onsubmit' => 'return false;'
    ));

?>
        <div class="span-8 required">
            <label for="FonykerBirthdate">Birthdate</label>
            <input id="FonykerBirthdate" type="text" onsubmit="return false;" name="data[Fonyker][birthdate]" class="datepicker input-text long" enabled="false" value="<?php echo $this->data['Fonyker']['birthdate']; ?>">
        </div>
<?php

    $options = array('M' => 'M', 'F' => 'F');
    $attributes = array(
      'empty' => 'Gender',
      'class' => 'input-combo',
      'label' =>  array(
        'text' => 'Birthdate'
      )
    );

    echo $this->Form->select('gender', $options, NULL, $attributes);

    echo $this->Form->submit('Save', array(
      'class' => 'button medium blue',
      'id' => 'save-account-button'
    ));

?>
    <input id="FonykerId" type="hidden" name="data[Fonyker][id]" value="<?php echo $this->data['Fonyker']['id']?>">
<?php
    echo $this->Form->end();
?>
<div id="account-message" class="span-8" style="display: none;"></div>
</div>

操作:

代码语言:javascript
复制
function edit() {
      $this->layout = 'ajax';
      $this->autoRender = false;
      $this->Fonyker->recursive = -1;
      $response = null;

      if (!empty($this->data)) {
        $this->Fonyker->read(null, $this->data['Fonyker']['id']);
        pr($this->data);

        if ($this->Fonyker->save($this->data)) {
          $response = json_encode(array('ok' => true, 'msg' => 'Account information updated'));
        } else {
          $response = json_encode(array('ok' => false, 'msg' => 'Failed to update account'));
        }
      }

模型动作作为代码:

代码语言:javascript
复制
var $actsAs = array(
    'MeioUpload' => array( 
      'image_url' => array(
        'dir' => 'img/profile_pictures',
        'create_directory' => true,
        'allowed_mime' => array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'),
        'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif','.JPG'),
      )
    )
  ); 

上传在我的应用程序的另一部分可以正常工作,但在这种情况下就不行了,有什么想法吗?

代码语言:javascript
复制
      echo $response;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-29 23:05:31

问题是,我是通过AJAX提交表单的,当我序列化表单时,文件并不包含在表单中,我是web开发的新手,所以我还不太了解所有东西的工作原理,但似乎你不能通过AJAX提交文件。

因此,我修改了我的代码,使其不需要AJAX请求就可以工作,只需使用POST正常提交表单,它就可以正常工作。

票数 2
EN

Stack Overflow用户

发布于 2011-08-23 07:00:37

下面是我要检查的几件事:

在控制器函数中,在保存之前,可以检查$this->data['image_url']的内容并查看error字段是否为非零。错误代码是枚举的in the PHP manual,可能会为您提供问题线索。

您也可以在没有MIME类型限制的情况下对其进行测试。MeioUpload可能是(?)依赖于浏览器报告的MIME类型,而MIME类型几乎肯定只基于文件扩展名-通常是可以的,但你永远不会知道。我也知道浏览器(我想是Safari吧?)将所有内容都报告为application/octet-stream。(您也可以使用PHP的FileInfo,它根据文件内容确定MIME类型。)

不管怎样,总会有个开始的地方。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7154415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档