使用jquery ajax和codeigniter上传csv文件 [英] uploading csv file using jquery ajax and codeigniter

查看:108
本文介绍了使用jquery ajax和codeigniter上传csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我要使用jquery ajax函数上传并移动csv文件.

here i want upload and move the csv file using jquery ajax function.

$("#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.");         
    }
  });   
});

我的html代码是:

< input type="file" class="form-control" id="file" name="file">

我正在测试控制器功能上载的文件名像$ this-> input-> post('file');

and i am testing in controller function uploaded file name like $this->input->post('file');

但是它还没有到达控制器,我想将该文件移到一个文件夹中.请帮我在我做错的地方...

but it is not come to the controller and i want to move that file into a folder. please help me where i am doing mistake...

我的服务器端代码是:

$curdate=date('Y-m-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))

推荐答案

您需要使用

You need to use FormData to send files over via AJAX.

将文件存储在FormData对象中,然后将该对象作为您的 data 传递.

Store the file in a FormData object and pass the object as your 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.");         
        }
    });
});

阅读文件上传类(尤其是从控制器中获取)部分),了解如何在服务器端处理文件.

Read File Uploading Class (particularly from The Controller section) on how to deal with the file on the server-side end.

这篇关于使用jquery ajax和codeigniter上传csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆