File Upload using AngularJS

前端 未结 29 2018
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

29条回答
  •  心在旅途
    2020-11-21 08:07

    You can use a FormData object which is safe and fast:

    // Store the file object when input field is changed
    $scope.contentChanged = function(event){
        if (!event.files.length)
            return null;
    
        $scope.content = new FormData();
        $scope.content.append('fileUpload', event.files[0]); 
        $scope.$apply();
    }
    
    // Upload the file over HTTP
    $scope.upload = function(){
        $http({
            method: 'POST', 
            url: '/remote/url',
            headers: {'Content-Type': undefined },
            data: $scope.content,
        }).success(function(response) {
            // Uploading complete
            console.log('Request finished', response);
        });
    }
    

提交回复
热议问题