Fill
Fill

Reputation: 98

Upload file not work

Does not work downloading the file, please tell me what is the problem?

<div ng-conroller="loadImgCtrl">
    <input type='file' id='file' name='file'>
    <button ng-click="upload()">Upload</button>
<div>

JS:

   var app=angular.module('loadImage', []);

    app.controller('loadImgCtrl', ['$scope', '$http', function ($scope, $http) { 
    $scope.upload = function(){

    var fd = new FormData();
    var files = document.getElementById('file').files[0];
    fd.append('file', files);

    $http({
        method: 'post',
        url: 'upload.php',
        data: fd,
        headers: {'Content-Type': undefined},
        }).then(function successCallback(response) {
        $scope.response = response;
    });
}
}]);

PHP:

<?php 
    $filename = $_FILES['file']['name'];
    $location = '/images';
    move_uploaded_file($_FILES['file']['tmp_name'],$location.$filename);

    $arr = array("name"=>$filename);
    echo json_encode($arr);
?>

In the response I receive: {"name":null}.
Version angularjs 1.3.5.

Upvotes: 0

Views: 49

Answers (1)

IzumiSy
IzumiSy

Reputation: 1528

You can try this code

$http.post('upload.php', fd, {
  headers: {
    'Content-Type': undefined
  },
  transformRequest: null
}).then(function successCallback(response) {
  $scope.response = response;
});

Setting transformRequest: null explicitly, you can prevent transforming the request into incorrect format like JSON.

Upvotes: 1

Related Questions