Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
3.1 kB
2
Indexable
Never
function getBase64(file) {
                return new Promise((resolve, reject) => {
                    const reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onload = () => resolve(reader.result);
                    reader.onerror = error => reject(error);
                });
            }

            $(document).on('click', '#verifikasiFile', async function() {
                var error = 0;
                var drh = $('#drh-file')[0].files[0];
                var salinan_ijazah = $('#salinan_ijazah-file')[0].files[0];

                if (!drh || !salinan_ijazah) {
                    error++;
                    $('#input_error').html("<?= $this->lang->line('student_file_required') ?>");
                    $('#input_error').parent().addClass('has-error');
                } else {
                    $('#input_error').html('');
                    $('#input_error').parent().removeClass('has-error');
                }

                if (error === 0) {
                    var studentID = "<?= $profile->studentID ?>";
                    var formData = new FormData($('#statusDocumentUploadDataForm')[0]);
                    formData.append("studentID", studentID);

                    try {
                        var drhBase64 = await getBase64(drh);
                        var salinanIjazahBase64 = await getBase64(salinan_ijazah);

                        formData.append('drh_base64', drhBase64);
                        formData.append('salinan_ijazah_base64', salinanIjazahBase64);

                        $.ajax({
                            type: 'POST',
                            url: "<?= base_url('student/statusDocumentUpload') ?>",
                            data: formData,
                            processData: false,
                            contentType: false,
                            dataType: "json",
                            success: function(response) {
                                if (response.status) {
                                    $('#errorMessagesFinal').hide().html('');
                                    location.reload();
                                } else {
                                    $('#errorMessagesFinal').show().html('');
                                    $.each(response.errors, function(key, value) {
                                        $('#' + key + '_error').html(value);
                                        $('#' + key + '_error').parent().addClass('has-error');
                                    });
                                }
                            },
                            error: function(xhr, status, error) {
                                console.error("AJAX Error:", error);
                                console.log("Response:", xhr.responseText);
                            }
                        });
                    } catch (error) {
                        console.error('Error converting files to base64:', error);
                    }
                }
            });
Leave a Comment