1
votes

I have created a page that uses Angular File Upload component (https://github.com/nervgh/angular-file-upload/wiki/Module-API) however, I can't find how to get the uploaded file info to save it on disk.

I don't know what's the key name and how to save it. Here is a sample of my code if you could please help me.

app.controller('HomeController', ['$scope', 'FileUploader', function ($scope, FileUploader) {
    $scope.uploadMessage = 'Arraste aqui o arquivo';

    var uploader = $scope.uploader = new FileUploader( { autoUpload: true, url: '/url'});

    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length <= 1;
        }
    });

    // Callbacks
    uploader.onAfterAddingFile = function (item) {
        $scope.uploadMessage = 'Enviando arquivo ' + item.file.name.toString();
    };

    uploader.onCompleteItem = function(item, response, status, headers) {

    };
}]);

And my servlet post function is just like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

The Angular component help says that there is a property called "alias" {String}: Name of the field which will contain the file, default is file. If I use request.getParameter("file") it's null.

UPDATE

I'm using @MultipartConfig annotation in my server however request.getPart is always null.

1
same like as calling from angular to any other api. see the example hmkcode.com/java-servlet-jquery-file-upload and stackoverflow.com/questions/28523453/…Shohel

1 Answers

0
votes

Need to change your url, It should be unique. Try to this

var uploader = $scope.uploader = new FileUploader({ autoUpload: true, url: '/upload' });

Server Side:

@RequestMapping(value = "/upload" , method = RequestMethod.POST)
public void upload(HttpServletRequest request)
{
    //org.springframework.web.multipart.MultipartHttpServletRequest
    MultipartHttpServletRequest mRequest;
    mRequest = (MultipartHttpServletRequest) request;

    Iterator<String> itr = mRequest.getFileNames();
    while (itr.hasNext()) {
        //org.springframework.web.multipart.MultipartFile
        MultipartFile mFile = mRequest.getFile(itr.next());
        String fileName = mFile.getOriginalFilename();
        System.out.println("*****"+ fileName);

        //To copy the file to a specific location in machine.
        File file = new File('path/to/new/location');
        FileCopyUtils.copy(mFile.getBytes(), file); //This will copy the file to the specific location.
    }
}