6

I have the following code:

app.controller("carousels", function($http, $location, $rootScope, $state, $stateParams, $scope, PopUp, DTColumnDefBuilder, DTOptionsBuilder) {

    { ... }

    $scope.save = function() {
        $http({
            method: "POST",
            url: "./controllers/carousels/create.php",
            headers: {
                "Content-Type": "multipart/form-data"
            },
            data: $scope.carousels,
            transformRequest: function (data, headersGetter) {
                var formData = new FormData(),
                    headers = headersGetter();

                angular.forEach(data, function (value, key) {
                    formData.append(key, value);
                });

                delete headers["Content-Type"];

                return formData;
            }
        }).success(function(response) {
            console.log(response);
        });
    };
});

The create.php file is completely empty and save function is called by ng-submit on the form tag.

<form name="form" ng-submit="save()">
    <div class="box box-default">
        <div class="box-header with-border"></div>

        <div class="box-body">
            <div class="form-group" has-error>
                <label for="image">{{ "txt.pages.carousels.fields.image" | translate }}*</label>
                <div class="fileinput fileinput-new input-group" data-provides="fileinput">
                    <div style="display: table-row;">
                        <div class="form-control" data-trigger="fileinput">
                            <i class="glyphicon glyphicon-file fileinput-exists"></i>
                            <span class="fileinput-filename"></span>
                        </div>
                        <span class="btn btn-default btn-file input-group-addon">
                            <span>{{ "txt.action.file" | translate }}</span>
                            <input type="file" accept="image/*" name="image" id="image" ng-model="carousels.image" ngf-select required>
                        </span>
                    </div>

                    <div class="fileinput-preview thumbnail" data-trigger="fileinput">
                        <img ng-src="./uploads/{{ carousels.image }}">
                    </div>
                </div>
            </div>
        </div>

        <div class="box-footer">
            <button type="submit" class="btn btn-default">{{ "txt.action.save" | translate }}</button>
        </div>
    </div>
</form>

After submitting the form, the console returns the following message:

Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

This code always worked properly in all my projects, but this time is giving this error and I'm unable to find a solution.
The only difference between this project and others, is that in this I am using UI-Router. I tried to look for a connection between them, but I found nothing.

Does anyone know what might be causing this error and how to fix it?

4 Answers 4

12

After a long time and looking at various functions over the Internet, just changed the header:

headers: {
    "Content-Type": "multipart/form-data"
},

To:

headers: {
    "Content-Type": undefined
},

And the code again function normally.

Thank you for the help.

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

Comments

1

I have resolve my issue to remove Content-Type from header and its working perfectly

1 Comment

Best answer... :)
0

Why are you deleting content-type in transformRequest if you're parsing it through the property "header"?

Did you try to not delete content-type?

5 Comments

The answer is simple: I copied the code. =) And yes, I've tried not delete. But nothing has changed. The error continues.
It's something coming from PHP. Maybe because the php file is empty. Try to capture the files with $_FILES or something like that. Also, can you share a code of the php file you're using in other projects?
That was the first thing I did. When I tested for the first time, the PHP script had been finalized. When I saw it was not working, I started to commenting the code to find out where was the error. The problem is that I just commenting the whole script and the error remained. I have also tried to empty the cache and use CCleaner, but nothing changed.
Julio, I already tested it and it works. I used <input type="file" name="carousels" ng-model="carousels" multiple> inside of <form ng-controller="carousels" ng-submit="save()"> and I get the response from PHP, which is <?php echo 'hola world';
I added an example of my form (just removed the other inputs). I'm using the plugins "jasny-bootstrap" and "ng-file-upload" as I always use. As I said, the only difference is that this time I'm using the UI-Router in place of ngRouter, but I can not find a connection in both nowhere.
-3

Instead of sending the data as multipart/form-data:

headers: {
    'Content-Type': "multipart/form-data' 
}

You should send it as application/json:

headers: {
    'Content-Type': 'application/json'
}

In case the problem still persists, you may also want to change data to params.

1 Comment

There is a big difference between the two.

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.