aquí quiero cargar y mover el archivo csv usando la función jquery ajax.
$("#file").change(function() { alert($("#file").val()); $.ajax({ url: "<?php echo base_url(); ?>index.php/Task_controller/upload_tasksquestion", type: "post", dataType: 'json', processData: false, contentType: false, data: {file: $("#file").val()}, success: function(text) { alert(text); if(text == "success") { alert("Your image was uploaded successfully"); } }, error: function() { alert("An error occured, please try again."); } }); });y mi código html es:
< input type="file" class="form-control" id="file" name="file">y estoy probando en la función del controlador el nombre del archivo cargado como $this->input->post('file');
pero no llega al controlador y quiero mover ese archivo a una carpeta. por favor ayúdenme donde estoy cometiendo un error...
mi código del lado del servidor es:
$curdate=date('Ym-d'); $path='./assets/fileuploads/'; $file=basename($_FILES['file']['name']); list($txt, $ext) = explode(".", $file); $actual_file_name = time().substr($txt, 5).".".$ext; if(move_uploaded_file($_FILES['file']['tmp_name'],$path.$actual_file_name))Debe usar FormData para enviar archivos a través de AJAX.
Almacene el archivo en un objeto FormData y pase el objeto como sus data .
$("#file").change(function() { var fd = new FormData(); fd.append('file', this.files[0]); // since this is your file input $.ajax({ url: "<?php echo base_url(); ?>index.php/Task_controller/upload_tasksquestion", type: "post", dataType: 'json', processData: false, // important contentType: false, // important data: fd, success: function(text) { alert(text); if(text == "success") { alert("Your image was uploaded successfully"); } }, error: function() { alert("An error occured, please try again."); } }); });Lea la Clase de carga de archivos (particularmente de la sección El controlador) sobre cómo manejar el archivo en el extremo del lado del servidor.