json - AngularJS Not Setting Correct Content-Type In Http POST -
so trying upload mp3 file nodejs server. seeing file data , correctly, know model right, every time send http post, invalid json error. confusing because have set content-type both undefinded , multipart/form-data , regardless when console still see in request header content-type set application/json. here code using upload:
template:
<form> <div class="form-group"> <label name="type-label" for="type">type</label> <select name="type" ng-model="data.type"> <option value="music">music</option> <option value="image">image</option> <option value="video">video</option> </select> </div> <div class="form-group"> <label name="name-label" for="name">name</label> <input type="text" name="name" ng-model="data.name" /> </div> <div class="form-group"> <label name="description-label" for="description">description</label> <input type="text" name="description" ng-model="data.description" /> </div> <div> <input type="file" upload-file="file"/> </div> <div class="form-group"> <div class="btn btn-primary" ng-click="upload()">select file</div> </div> </form>
service:
angular.module('app').factory("uploadservice", ['$http', function($http) { return { upload: function (file, data) { var fd = new formdata(); fd.append('file', file); return $http({ method: 'post', url: '/upload/' + data.type, data: fd, transformrequest: angular.identity, headers: { 'content-type': 'multipart/form-data' } }) .success(function(){ //do something. }) .error(function(){ //do something. }); } } } ]);
controller:
angular.module('app').controller('uploadcontroller', ['$scope', 'uploadservice', function($scope, uploadservice) { $scope.data = {}; $scope.upload = function() { console.log($scope.file); uploadservice.upload($scope.file, $scope.data); }; } ]);
in end wrote little helper config try , force default content-type http posts.
helper:
angular.module('app').config(function($httpprovider) { $httpprovider.defaults.headers.post = { 'content-type' : 'multipart/form-data' }; });
at point pretty stuck on do, suggestions/help appreciated. or tips on uploading mp3s in general if there easier way this. thanks!
try this
$httpprovider.defaults.headers.post['content-type'] = 'multipart/form-data; charset=utf-8';
Comments
Post a Comment