Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIle upload from a rest client to a rest server

I created a rest server using the codeigniter rest server library created by PhilSturgeon :

github.com/philsturgeon/codeigniter-restserver

Now, I am using Codeignitor rest client :

github.com/philsturgeon/codeigniter-restclient

to get/post data from/to the rest server and am successfully able to get/post normal data.

What is the best way to post image files to the rest server from the rest client ?

Also, how can someone can access the API and perform all get/post operation from C# .NET assuming the rest server uses digest authentication ? [any library available ?]

like image 405
Amit Aggarwal Avatar asked Dec 29 '10 05:12

Amit Aggarwal


2 Answers

Simple Rest Post service to upload & store a file from any Rest client

public function product_post()
{
    $uploaddir = './uploads/products/';
    $file_name = underscore($_FILES['file']['name']);
    $uploadfile = $uploaddir.$file_name;

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        $dataDB['status'] = 'success';       
        $dataDB['filename'] = $file_name;
     } else {
        $dataDB['status'] =  'failure';       
     }
     $this->response($dataDB, 200);
}
like image 108
Srivaths Avatar answered Sep 19 '22 08:09

Srivaths


After searched for more than 1 days, finally this is what worked for me

public function upload_post()
    {
      $config['upload_path'] = './uploads/';
      $config['allowed_types'] = '*';

      $this->load->library('upload');
      $this->upload->initialize($config);

      if ( ! $this->upload->do_upload('file'))
      {
        $error = array('error' => $this->upload->display_errors());
         print_r($error);
      //  $this->load->view('upload_form', $error);
      }
      else
      {
        $data = array('upload_data' => $this->upload->data());
        print_r($data);
      //  $this->load->view('upload_success', $data);
      }
    }
like image 43
G.Ashok Kumar Avatar answered Sep 20 '22 08:09

G.Ashok Kumar