【发布时间】:2014-12-13 23:04:26
【问题描述】:
我正在尝试在 CodeIgniter 中上传带有 file 字段的 form。我成功地将没有file 的表单数据插入数据库。但是当我尝试使用文件字段时,它会显示以下错误
`<p>You did not select a file to upload.</p>`
我尝试了很多方法,例如 this forum thread 和 this SO question,但没有任何帮助。
view_add_author.php
<?php
$this->load->helper('form');
echo form_open_multipart('admin/form_actions/add_author', 'id="add_author_form"');
?>
<div class="form-group">
<label for="author_image">Image</label>
<input type="file" name="author_image" id="author_image">
<p class="help-block">Format(jpg, jpeg, gif, png)</p>
</div>
custom.js
function add_author(data){
return $.ajax({
url: url+'admin/form_actions/add_author',
type: 'POST',
async: false,
dataType: 'json',
mimeType:"multipart/form-data",
data: data
});
}
$('#add_author_form').submit(function(e){
e.preventDefault();
var data = $('#add_author_form').serialize();
add_author(data).done(function(data){
if(data.msg != '')
{
$('#add_author_form')[0].reset();
alert(data.msg);
}
else if(data.error != '')
{
alert(data.error);
}
});
});
form_actions.php
public function add_author ()
{
$this -> load -> helper ( 'form' );
$config = array (
'upload_path' => './images/author/',
'allowed_types' => 'gif|jpg|jpeg|png',
'max_size' => '2000',
'max_width' => '2000',
'max_height' => '2000',
'encrypt_name' => true,
);
$this -> load -> library ( 'upload', $config );
if ( ! $this -> upload -> do_upload ( 'author_image' ) )
{
echo $error = $this -> upload -> display_errors ();
}
else
{
echo $data = $this -> upload -> data ();
}
}
【问题讨论】:
标签: ajax codeigniter file-upload