How can we upload the binary file(.bin) through angular and handle it in backend with code igniter

436 Views Asked by At

I'm creating an application in which the user will upload a binary file onto the server and the admin can view and verify the file. But the problem I am having is that the FileUploader class in angular is not accepting the file type .bin and not communicating it to my codeigniter backend server.

I have already tried multiple extension forms in the code like bin,application/octet-stream and even '*' so that it can accept all types of files. But none of it worked

MY angular code to upload file using FileUploader

var id = ($scope.id = new FileUploader({
      queueLimit: 1,
      url: "server/index.php/Upload/file"
    }));

// FILTERS

// a sync filter
id.filters.push({
  name: "syncFilter",
  fn: function(item /*{File|FileLikeObject}*/, options) {
    if (this.queue.length > 0) {
      id.clearQueue();
    }
    return this.queue.length < 1;
  }
});

// an async filter
id.filters.push({
  name: "asyncFilter",
  fn: function(item /*{File|FileLikeObject}*/, options, deferred) {
    setTimeout(deferred.resolve, 1e3);
  }
});

id.filters.push({
  name: "imageFilter",
  fn: function(item /*{File|FileLikeObject}*/, options) {
    var type = "|" + item.type.slice(item.type.lastIndexOf("/") + 1) + "|";
    return "|bin|".indexOf(type) !== -1;
  }
});

MY codeigniter file funtion which uploads the file and in return responds with its name

public function file() {
        $data = array();
        $config['upload_path'] = 'public/files';
        $config['allowed_types'] = 'bin';
        $config['max_size'] = 10000000;
        $config['max_width'] = 1920;
        $config['max_height'] = 1280;
        $file_name = $_FILES["file"]['name'];
        $newfile_name = preg_replace('/[^A-Za-z0-9]/', "", $file_name);

        $config['file_name'] = time() . $newfile_name;
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');


        $data = $this->upload->data();
        echo json_encode($data);
        exit();
    }
0

There are 0 best solutions below