我在上传文件时遇到问题,并将其保存在我指定的文件夹中。我有来自其他网站的教程,我编辑的代码不起作用。我是CodeIgniter框架。我的控制台中也出现错误,错误为
"未捕获的TypeError:$ .ajaxFileUpload不是函数"
这是我的HTML:
<input type="file" name="userfile" class="file" id="userfile">
<div class="input-group col-xs-4">
<span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span>
<input type="submit" name="submit" id="submit" />
<span class="input-group-btn">
<button class="browse btn btn-primary input-sm" type="button"><i class="glyphicon glyphicon-search"></i> Browse</button>
</span>
</div>
这也是我的脚本。我的openfiledialog有两个脚本并上传提交按钮:
<script>
$(document).on('click', '.browse', function() {
var file = $(this).parent().parent().parent().find('.file');
file.trigger('click');
});
$(document).on('change', '.file', function() {
$(this).parent().find('.form-control').val($(this).val().replace(/C:\\fakepath\\/i, ''));
});
</script>
<script>
$(function() {
$('#upload_file').submit(function(e) {
e.preventDefault();
$.ajaxFileUpload({
url: '<?php echo site_url('
manager / profile / upload_file ')?>',
secureuri: false,
fileElementId: 'userfile',
dataType: 'json',
success: function(data, status) {
alert(data.msg);
}
});
return false;
});
});
</script>
这是我的控制器将图像文件保存在我的文件夹中:
public function upload_file() {
$status = "";
$file_element_name = 'userfile';
if ($status != "error") {
$config['upload_path'] = APPPATH . 'images/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name)) {
$status = 'error';
} else {
$data = $this->upload->data();
}
@unlink($_FILES[$file_element_name]);
}
echo json_encode(array('status' => $status));
}