0

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

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

看法:

<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>

行动:

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

模型作为代码:

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'),
      )
    )
  ); 

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

      echo $response;
    }
4

2 回答 2

2

问题是我通过 AJAX 提交表单,当我序列化表单时,文件不包含在其中,我是 web 开发的新手,所以我还不太了解一切的工作原理,但看起来你无法通过 AJAX 提交文件。

所以我修改了我的代码以在没有 AJAX 请求的情况下工作,只需使用 POST 正常提交表单,它就可以正常工作。

于 2011-08-29T15:05:31.017 回答
1

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

在您的控制器功能中,在保存之前,您可能会检查内容$this->data['image_url']并查看该error字段是否非零。PHP 手册中列举了错误代码,可能会为您提供问题的线索。

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

无论如何,从哪里开始。

于 2011-08-22T23:00:36.900 回答