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