Angularjs file upload with formData send Code to Codeigniter Service

826 views
Skip to first unread message

chakrala

unread,
Sep 30, 2015, 8:04:24 AM9/30/15
to AngularJS
 I am very new to Angular so forgive me first of all :)

I am coming from a php MVC Codeigniter. But I am unable to insert username and file in to Database(Mysql) using Codeigniter Service,but my file is uploading
here is my factory code:
Controller.js Code
app.factory("services", ['$http', function($http) {
 var fd = new FormData();
 for ( var i = 0; i < files.length; i++)
 {
  fd.append("file", files[i]);
 }
// var data1={id:id};
  fd.append("username", username);
  $http.post(url, fd, {
  
   withCredentials : false,
  
   headers : {
    'Content-Type' : undefined
   },
 transformRequest : angular.identity
 });
}
Php Code
function uploadfile(){
$target_dir = "upload/avatar/";
$filename = "koti"."-"."raju"."-".basename($_FILES["file"]["name"]);
$target_file = $target_dir . $filename;
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
 
        $json_register= file_get_contents('php://input');
$registerdata = json_decode($json_register,true);
$post_username = $registerdata['username'];
 $arr = array(
'profilepic'=>$filename  
);
$check =  $this->register_model->edit($arr,$post_donor_id);
    if($check > 0) 
{
$msg = "Data Updated Successfully";
header('Content-type: application/json');
$data = array('status' => "0",'desc' => $msg);
echo json_encode($data);
}

}

Sander Elias

unread,
Sep 30, 2015, 9:30:35 AM9/30/15
to AngularJS
Hi chakrala,

This has little to do with angular and a lot with how PHP handles multi-part forms. You should ask srround on a PHP form how to properly decode an multipart form.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages