1

I am using Angularjs 1.5, and trying to upload multiple files but in http request unable to send file data to java service, file data going as empty object.

I am using the following directory to upload the multiple files:

myForm.directive('ngFileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.ngFileModel);
            var isMultiple = attrs.multiple;
            var modelSetter = model.assign;
            element.bind('change', function () {
                var values = [];
                angular.forEach(element[0].files, function (item) {
                    var value = {                       
                        name: item.name,                       
                        size: item.size,                       
                        url: URL.createObjectURL(item),
                        // File Input Value 
                        _file: item
                    };
                    values.push(value);
                });
                scope.$apply(function () {
                    if (isMultiple) {
                        modelSetter(scope, values);
                    } else {
                        modelSetter(scope, values[0]);
                    }
                });
            });
        }
    };
}]);

On http request _file object going as empty object. 1. How can I send file data in _file object?

Query String Parameters:

param: { "files": [{"name":"arrowred.png","size":34516,"url":"blob:http://localhost:7001/24c4d435-3cab-410e-9bf4-8bdce7990f4d","_file":{}}]}

10
  • I don't think you can bind file to a model, so it's empty. But sending it should be fine; all of your files are in values array. So just send that to your back-end Commented Jun 12, 2018 at 10:46
  • Hi Aleksy, thanks for your response, how can I save my file data, blob is temporary browser URL. how can I convert blob URL to data object? please help on this. Commented Jun 12, 2018 at 10:57
  • Don't convert the files to data URLs. Also don't use the ng- prefix for custom directives. The ng- prefix is reserved for core AngularJS directives. Commented Jun 12, 2018 at 22:24
  • @georgeawg thanks for your response, how can I convert blob URL to bite array in Java(EJB I am using) to save in DB? Please suggest. Commented Jun 13, 2018 at 11:12
  • See How to POST binary files with AngularJS. Don't convert the file to anything. Send it directly. Commented Jun 13, 2018 at 14:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.