I have an input to upload an excel file and a hidden input
<input type="file" id="file">
<input type="hidden" id="file_submit">
When I choose file in input type file, I want to set its value to hidden input below and retrieve in AJAX function, then I will reset input file for next upload, prevent ERR_UPLOAD_FILE_CHANGED
error.
Here is my code in AJAX:
var formData = new FormData();
formData.append('file', $('#file_submit').val());
$.ajax({
url: "{{ route('admin.handle-import') }}",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
method: "POST",
data: formData,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
success: function(res) {
$('file').val(''); //reset input file
}
});
I tried this code to set value for hidden input:
document.getElementById('file').onchange = function() {
$('#file_submit').val($(this).prop('files'));
};
And in my controller, I get by code $request->file;
. But it only return "[object FileList]"
. I really don't know what I have to do now.
Can you give me a solution for this?
Thank you very much