2011-08-22 63 views
0

我试图上传一个图像文件到我的应用程序,用户可以选择通过上传图像来更改他的个人资料图片,但是当表单被提交时它不会提交文件表格。CakePHP - 表单不提交文件字段

我使用的是韦上传我的文件上传,我会贴上我的视图控制器动作和模型:

查看:

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

型号actsAs代码:

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

回答

1

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

因此,我修改了我的代码,没有AJAX请求,只是通常提交表单提交表单,它工作正常。

+0

可以通过AJAX进行文件上传。请参阅http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload的答案。 – SunnyD

1

这里有几件事情我会检查:

在你的控制器功能,为您节省之前,你可能要检查的$this->data['image_url']内容,看看error字段是非零。错误代码枚举in the PHP manual,可能会给你一个线索的问题。

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

无论如何,某处开始。

+1

没有image_url的内容,这是问题,它不是与表单一起提交,在其他情况下,在我的项目中,它工作得很好 – 8vius