2

I'm writing an angular-1.5.0-rc0 application

In this application I create FormData, fill it with values and I need to send it with $http request. for now I use jquery ajax with the following code:

this.addDrink = function () {
        var data = new FormData();
        var drinkBrand = caller.drink.drinkBrand;
        var drinkType = caller.drink.drinkType;
        var drinkFlavor = caller.drink.drinkFlavor;
        var liquidColor = caller.drink.liquidColor;
        var drinkCompany = caller.drink.drinkCompany;
        var liquidIsTransparent = caller.drink.liquidIsTransparent;
        var drinkImage = caller.drink.drinkImage;
        var caloriesFor100g = caller.drink.caloriesFor100g;
        var alcoholSum = caller.drink.alcoholSum;
        var alcoholSumType = caller.drink.alcoholSumType;
        data.append('alcohol_sum_type', alcoholSumType);
        data.append('drink_brand', drinkBrand);
        data.append('drink_type', drinkType);
        data.append('drink_flavor', drinkFlavor);
        data.append('liquid_color', liquidColor);
        data.append('liquid_is_transparent', liquidIsTransparent);
        data.append('drink_image', drinkImage);
        data.append('drink_company', drinkCompany);
        data.append('calories_for_100g', caloriesFor100g);
        data.append('alcohol_sum', alcoholSum);
        $.ajax({
            url: 'https://api.myalcoholist.com:8888/drink',
            data: data,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function (data) {
                if (!data.success) {
                    alert(data.data);
                } else {
                    alert('done');
                }
            }
        });
    };

as you can see I set processData and ContentType to false in order to be able to send FormData in jquery ajax call. how can it be done with $http?

4
  • hello-angularjs.appspot.com/… Commented Dec 28, 2015 at 6:30
  • thanks @AnikIslamAbhi but it seems that the example there is for sending json data. i want to send FormData since i'm also attaching an image Commented Dec 28, 2015 at 6:55
  • uncorkedstudios.com/blog/… Commented Dec 28, 2015 at 6:58
  • 1
    You can use some 3rd party lib to facilitate file uploads, i.e. ng-file-upload. Commented Dec 28, 2015 at 7:45

1 Answer 1

3
$scope.addDrink = function(files) {

// append all what u want.

$http.post('https://api.myalcoholist.com:8888/drink', data, {       
    headers: {'Content-Type': undefined },
    transformRequest: angular.identity
}).success( ...all right!... ).error( ..damn!... );

};
Sign up to request clarification or add additional context in comments.

1 Comment

This works great, Most important thing to consider (if you forgot read the original question). Transform your ng-model to formData Object. Systems made with .net need this type of convertion, i Dunno why. :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.