Upload file dengan ajax, codeigniter

function simpanBA() {
	var ba        = $("select#ba").val();
	var pekerjaan = $("input#pekerjaan").val();
	var id_und    = $("select#undangan").val();
	var no_srt    = $("input#no_srt").val();
	var lampiran  = $("#lampiran").val();

	// Cek textbox kosong
	if (no_srt == '') {
		alert('Nomor Surat belum diisi.');
		$("input#no_srt").focus();
	} else if (id_und == '') {
		alert('Tanggal Rapat belum diisi.');
		$("select#undangan").focus();
	} else if (lampiran == '') {
		alert('Lampiran belum diisi.');
		$("select#la").focus();
	} else {

		$.ajax({
			type     : "POST",
			dataType : "html",
			url      : "<?php echo site_url();?>/admin/ba/simpanBA",
			data     : "ba="+ba+"&pekerjaan="+pekerjaan+"&no_srt="+no_srt+"&id_und="+id_und+"&lampiran="+lampiran,

			success: function(msg){
				alert(msg);
				loadTabel();
			}
		});
	}
}
public function simpanBA()
	{
		// Load model
		$this->model_squrity->getSqurity();

		$id_und = $this->input->post('id_und');
		$query = $this->model_data->getByID('tb_file', 'id', $id_und)->row();
		$lampiran = $this->input->post('lampiran');

		// Tampung variabel
		$data['id']        = '';
		$data['pekerjaan'] = $this->input->post('pekerjaan');
		$data['jns_ba']    = $this->input->post('ba');
		$data['ba_und']    = 'Berita Acara';
		$data['no_srt']    = $this->input->post('no_srt');
		$data['tgl_surat'] = date('Y-m-d');
		$data['tgl_acara'] = $query->tgl_acara;
		$data['waktu']     = $query->waktu;
		$data['lokasi']    = $query->lokasi;

		//upload config
		$config['upload_path'] 		= './upload/lampiran';
		$config['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
		$config['max_size']			= '2000';
		$config['max_width']  		= '3000';
		$config['max_height'] 		= '3000';

		$this->load->library('upload', $config);		// Load library upload

		if ($this->upload->do_upload($lampiran)) {
			// Periksa apakah upload berhasil
			// Jika berhasil
			// $up_data	 	= $this->upload->data();
			// $data['lampiran']	= $up_data['file_name'];

			// $this->model_crud->getInsert('tb_file', $data);                      // Menambahkan record baru
			echo "Data berhasil disimpan";
		} else {
			// $this->model_crud->getInsert('tb_file', $data);                      // Menambahkan record baru
			echo "Data berhasil disimpan, Upload gagal";
		}
	}

hasilnya upload gagal terus masta.. kira-kira apa yang salah... ada ada yg punya referensi code yang lebih mudah

avatar Khairun
@Khairun

10 Kontribusi 3 Poin

Diperbarui 7 tahun yang lalu

3 Jawaban:

buat referensi ajax codeigniter upload file : http://jagocoding.com/tutorial/851/Jquery_AJax_File_Upload_Codeigniter

avatar gunalirezqimauludi
@gunalirezqimauludi

350 Kontribusi 243 Poin

Dipost 7 tahun yang lalu

sebelum saya nulis disini saya sudah lihat tutor itu mas tp gx berhasil disaya..

avatar Khairun
@Khairun

10 Kontribusi 3 Poin

Dipost 7 tahun yang lalu

Jawaban Terpilih

ini mas udah saya coba, buat referensi.. semogga ngebantu.. permasalahannya..

view html

<html>

<head>
    <title>Upload Form</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
    <form action="#" id="uploadForm">
        <input type="file" id="userfile" name="userfile" size="20" />
        <input type="submit" value="upload" />
    </form>
    <script type="text/javascript">
        $("#uploadForm").submit(function(event) {
            $.ajax({
                url: "http://localhost/forumsk/upload_ajax/index.php/welcome/do_upload", // Url to which the request is send
                type: "POST", // Type of request to be send, called as method
                data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                contentType: false, // The content type used when sending data to the server.
                cache: false, // To unable request pages to be cached
                processData: false, // To send DOMDocument or non processed data file it is set to false
                success: function(data) // A function to be called if request succeeds
                    {
                        alert("Data Loaded: " + data);
                    }
            });
        });
    </script>
</body>

</html>

controller - ci

public function do_upload()
    {
        if (isset($_FILES['userfile'])) {
            if ($_FILES['userfile']['name'] != '') {
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';

                $this->load->library('upload', $config);

                if ($this->upload->do_upload('userfile')) {
                    $this->upload->data();
		    print_r($this->upload->data());exit();

                    return 'success';
                } else {
                    return 'error';
                }
            } else {
                return 'error';
            }
        } else {
            return 'error';
        }
    }

avatar gunalirezqimauludi
@gunalirezqimauludi

350 Kontribusi 243 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban