My angular ts code -
fileChange(event) {
   // let fileList: FileList = event.target.files;
   let fileList: FileList = event.srcElement.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('uploadFile', file, file.name);
        console.log(file);
        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'text/html');
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
      //  let options = new RequestOptions({ headers: headers });
        this.http.post(`http://funiks.com/adminv8/products-api/upload.php`, 
formData, { headers: headers })
            .map(res => res.json())
           // .catch(error => Observable.throw(error))
            .subscribe(
                data => console.log('success'),
                error => console.log(error)
            )
    }
}





My php code -

<?php
    error_reporting(0);
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: 
Content-Type,x-prototype-version,x-requested-with');
    header('Cache-Control: max-age=900');
    header("Content-Type: application/json"); // tell client that we are 
sending json data


    $response = array();
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            $response['status']= "File is an image - " . $check["mime"] . 
".";
            $uploadOk = 1;
        } else {
            $response['status']= "File is not an image.";
            $uploadOk = 0;
        }


    if (file_exists($target_file)) {
        $response['status'] ="Sorry, file already exists.";
        $uploadOk = 0;
    }else if ($_FILES["fileToUpload"]["size"] > 500000) {
        $response['status'] = "Sorry, your file is too large.";
        $uploadOk = 0;
    }else if($imageFileType != "jpg" && $imageFileType != "png" && 
$imageFileType != "jpeg" && $imageFileType != "docx"
    && $imageFileType != "gif" ) {
         $response['status'] =  "Sorry, only JPG, JPEG, PNG & GIF files are 
allowed.";
        $uploadOk = 0;
    }else if ($uploadOk == 0) {
         $response['status'] =  "Sorry, your file was not uploaded.";


    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
$target_file)) {
             $response['status'] =  "The file ". basename( $_FILES[
"fileToUpload"]["name"]). " has been uploaded.";
        } else {
             $response['status'] =  "Sorry, there was an error uploading 
your file.";
        }


    }


    echo json_encode($response['status']);
    exit;
    }
    ?>


*When i try to upload, its not uploading, you can try here -*
*open - http://funiks.com/adminv8/ and then click on add then choose file*

*file should be uploaded here-*

*http://funiks.com/adminv8/products-api/uploads/*

*PLEASE HELP..*


-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to