目前,在我的项目中,用户可以上传附件并将其删除
当我要删除最后一个附件时,即使从数据库和上传文件夹中删除它仍会显示在表格列表中
问题:当用户删除最后一个附件时,如何确保它不显示。
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click', '#delete_button', function () {
$.ajax({
url: "<?php echo base_url('extensions/attachments/delete');?>",
type: 'post',
dataType: 'json',
data: {
attachment_id: $(this).attr("data-id"),
posthash: $('#posthash').val()
},
success: function(json) {
if (json['success'] == true) {
$.each(json['attachments'], function( key, value ) {
info = '<tr>';
info += '<td>';
info += value['orig_name'];
info += '</td>';
info += '<td>';
info += value['file_size'] + 'KB';
info += '</td>';
info += '<td class="text-center">';
info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
info += '</td>';
info += '</tr>';
});
$('#file-attachments tbody').html(info);
}
},
});
});
$('#add_attachment').on('click', function() {
$('#form-upload').remove();
$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
$('#form-upload input[name=\'file\']').trigger('click');
$('#form-upload input[name=\'file\']').on('change', function() {
var formData = new FormData($(this).parent()[0]);
formData.append('posthash', $("#newreply #posthash").val());
$.ajax({
url: "<?php echo base_url('extensions/attachments/upload');?>",
type: 'post',
dataType: 'json',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['success']) {
$.each(json['attachments'], function( key, value ) {
info = '<tr>';
info += '<td>';
info += value['orig_name'];
info += '</td>';
info += '<td>';
info += value['file_size'] + 'KB';
info += '</td>';
info += '<td class="text-center">';
info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
info += '</td>';
info += '</tr>';
});
$('#file-attachments tbody').append(info);
}
},
});
});
});
});
</script>
控制器功能
public function delete()
{
$data['attachments'] = array();
$data = array('success' => false);
if ($this->input->post('attachment_id'))
{
$data['success'] = true;
$get = $this->attachment_model->getattachment($this->input->post('attachment_id'));
if (unlink(FCPATH . 'uploads/' . $get['path']))
{
$this->attachment_model->deleteattachment($this->input->post('attachment_id'));
}
$attachments_results = $this->attachment_model->getattachmentsfornewreply($get['posthash']);
foreach ($attachments_results as $attachment)
{
$data['attachments'][] = array(
'attachment_id' => $attachment['attachment_id'],
'post_id' => $attachment['post_id'],
'posthash' => $attachment['posthash'],
'file_name' => $attachment['file_name'],
'orig_name' => $attachment['orig_name'],
'file_size' => $attachment['file_size'],
'path' => $attachment['path']
);
}
}
echo json_encode($data);
}
答案 0 :(得分:0)
我现在已经开始工作了,我必须创建一个javascript函数
哪个可以获取附件,然后在另一个ajax上我调用此函数$(document).ready(function() {
function getattachments() {
$.ajax({
url: "<?php echo base_url('extensions/attachments/getattachments');?>",
type: 'post',
dataType: 'json',
data: {
posthash: $('#posthash').val()
},
success: function(json) {
info = '';
$.each(json['attachments'], function( key, value ) {
info += '<tr>';
info += '<td>';
info += value['orig_name'];
info += '</td>';
info += '<td>';
info += value['file_size'] + 'KB';
info += '</td>';
info += '<td class="text-center">';
info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
info += '</td>';
info += '</tr>';
});
$('#file-attachments tbody').html(info);
},
});
}
$(document).on('click', '#delete_button', function (e) {
e.preventDefault();
$.ajax({
url: "<?php echo base_url('extensions/attachments/delete');?>",
type: 'post',
dataType: 'json',
data: {
attachment_id: $(this).attr("data-id"),
posthash: $('#posthash').val()
},
success: function(json) {
return getattachments();
},
});
});
$('#add_attachment').on('click', function() {
$('#form-upload').remove();
$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
$('#form-upload input[name=\'file\']').trigger('click');
$('#form-upload input[name=\'file\']').on('change', function() {
var formData = new FormData($(this).parent()[0]);
formData.append('posthash', $("#newreply #posthash").val());
$.ajax({
url: "<?php echo base_url('extensions/attachments/upload');?>",
type: 'post',
dataType: 'json',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(json) {
return getattachments();
},
});
});
});
});
当然,现在我必须创建一个新的php codeigniter控制器函数,它只能获取附件
<?php
class Attachments extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('extensions/attachment_model');
}
public function getattachments(){
$data['attachments'] = array();
$attachments_results = $this->attachment_model->getattachmentsfornewreply($this->input->post('posthash'));
foreach ($attachments_results as $attachment)
{
$data['attachments'][] = array(
'attachment_id' => $attachment['attachment_id'],
'post_id' => $attachment['post_id'],
'posthash' => $attachment['posthash'],
'file_name' => $attachment['file_name'],
'orig_name' => $attachment['orig_name'],
'file_size' => $attachment['file_size'],
'path' => $attachment['path']
);
}
echo json_encode($data);
}
public function upload()
{
$this->load->library('upload');
$data = array();
$attachments_results = array();
// The session userid
$user_id = $this->session->userdata('user_id');
// User Upload Folder
$user_upload_folder = $this->user_model->getuseruploadfolder($user_id);
$folder = date('Ym');
if (!is_dir(FCPATH . 'uploads/' . $folder)) {
mkdir(FCPATH . 'uploads/' . $folder, 0777, true);
}
$this->load->helper('string');
$config['upload_path'] = './uploads/' . $folder . '/';
$config['allowed_types'] = 'gif|jpg|png|php|JPEG|PNG';
$config['max_size'] = 5000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = 'post_' . $user_id . '_' . time() . '_' . random_string('alpha', 16);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file'))
{
$data['error'] = $this->upload->display_errors();
} else {
$upload_data = $this->upload->data();
$attachment_insert = array(
'post_id' => '0',
'posthash' => $this->input->post('posthash'),
'user_id' => $this->session->userdata('user_id'),
'file_name' => $upload_data['file_name'],
'orig_name' => $upload_data['client_name'],
'file_size' => $upload_data['file_size'],
'path' => $folder . '/' . $upload_data['file_name'],
'datecreated' => time()
);
$this->attachment_model->insert($attachment_insert);
$data['success'] = TRUE;
}
echo json_encode($data);
}
public function delete()
{
$data = array();
if ($this->input->post('attachment_id'))
{
$data['success'] = true;
$get = $this->attachment_model->getattachment($this->input->post('attachment_id'));
if (unlink(FCPATH . 'uploads/' . $get['path']))
{
$this->attachment_model->deleteattachment($this->input->post('attachment_id'));
}
}
echo json_encode($data);
}
}
控制器
&[u8]