1

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

1 Answer 1

0

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.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using a unique url already. Will I have to add Spring dependency just to make this work?

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.