使用ajax在codeigniter中上传文件

时间:2017-10-02 07:58:25

标签: php ajax codeigniter file-upload codeigniter-3

我正在尝试使用codeigniter框架中的ajax上传文件。我的代码在不使用ajax的情况下工作但是当我使用ajax时,我收到错误消息'Undefined index:picture'in if($ _ FILES ['picture'] ['name'])。

请检查此代码

查看:

<form enctype="multipart/form-data" method="post">
   <div class="form-group">
      <label for="int">Picture</label>
      <input type="file" id="picture" name="picture" class="dropify" data-height="300" />
   </div>
</form>

AJAX:

var picture=new FormData( $("#picture")[0] );
var url = "<?php echo site_url('Workscontroller/create_action'); ?>";  
            $.ajax({
              url:url,
              data: {"title":title,"caption":caption,"description":description,"kategori":kategori,"picture":picture},
              dataType:"JSON",
              type:"POST",
              mimeType: "multipart/form-data",
              contentType: false,  
              cache: false,  
              processData:false,  
              success:function(data){  
               swal("Berhasil ditambahkan!", "Anda berhasil menambahkan porto folio.", "success")
               window.location.replace(data.url);
           }  
       }); 

控制器:

$this->load->library('upload');
   $this->_rules();
    $nmfile = "file_".time(); //nama file saya beri nama langsung dan diikuti fungsi time
    $config['upload_path'] = './works/'; //path folder
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
    $config['max_size'] = '2048'; //maksimum besar file 2M
    $config['max_width']  = '2000'; //lebar maksimum 1288 px
    $config['max_height']  = '2000'; //tinggi maksimu 768 px
    $config['file_name'] = $nmfile; //nama yang terupload nantinya
    $this->upload->initialize($config);
    if($_FILES['picture']['name'])
    {
        if ($this->upload->do_upload('picture'))
        {
           $gbr = $this->upload->data();
           $data = array(
              'title' => $this->input->post('title',TRUE),
              'caption' => $this->input->post('caption',TRUE),
              'description' => $this->input->post('description',TRUE),
              'picture' => $gbr['file_name'],
              'kategori' => $this->input->post('kategori',TRUE),
              );
           $this->WorksModel->insert($data);
       }
   }
   else{

   }

2 个答案:

答案 0 :(得分:0)

formData的参数应该是HTML <form>元素。通过赋予<form> id属性,最容易做到这一点。

视图:

<form enctype="multipart/form-data" method="post" id='myForm'>

然后ajax改为

var formData = new FormData($("#myForm")[0]);

在javascript中,您没有显示如何设置titlecaptiondescriptionkategori的值。但它们显然是形式中的其他<input>元素。您可能不需要单独捕获这些值,因为所有表单输入(包括FILE类型输入)都在var formData中捕获。这意味着可以从

重写ajax data选项
data: {"title":title,"caption":caption,"description":description,"kategori":kategori,"picture":picture},

data: formData, 

if($_FILES['picture']['name'])现在可以正常工作。

答案 1 :(得分:0)

//像这样更新

var url =“”;
            $就({               网址:网址,               数据: “标题”:标题, “标题”:标题, “说明”:说明书中, “驾驶员学校”:驾驶员学校, “图片”:图像},               数据类型: “JSON”,               键入: “POST”,               mimeType:“multipart / form-data”,               contentType:false,
              async:false,             cache:false,             contentType:false,             processData:false,
              成功:功能(数据){
               swal(“Berhasil ditambahkan!”,“Anda berhasil menambahkan porto folio。”,“成功”)                window.location.replace(data.url);            }
       });