Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending file to Spring Controller #342

Closed
LFJustin opened this issue Sep 26, 2014 · 7 comments
Closed

Sending file to Spring Controller #342

LFJustin opened this issue Sep 26, 2014 · 7 comments

Comments

@LFJustin
Copy link

Hi Danial,
I am trying to use your file uploader to send a file to a Spring Controller. I am using the upload.http() from issue #88 but that is not working for me. I think I may just be getting the type wrong on what the controller expects.

@danialfarid
Copy link
Owner

For Spring Controller you can use $upload.upload() with this syntax:

    @RequestMapping(value = "/{id}/logo", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    public ImageUploadResponse upload(@PathVariable("id") Long id, @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
   file.getInputStream()
}

@LFJustin
Copy link
Author

That worked. Thank you for the help I really appreciate it.

@LFJustin
Copy link
Author

Actually after some more testing I tried to upload this to our server and testing on the server did not work. It tells me bad request. I am making other calls to the same Spring Controller and all of them are working.
Here is my Angular Controller code.
$scope.onFileSelect = function($files){
for(var i = 0; i < $files.length; i++){
var file = $files[i];
console.log("THE FILE " + file);
$scope.upload = $upload.upload({
url: './../../user-home/new-model',
method: 'POST',
data: {myObj: $scope.myModelObj},
file: file
}).progress(function (evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (ex) {
console.log("It Broke " + ex);
});
}
};

and here is my Spring Controller Code.
@RequestMapping(value = "user-home/new-model", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
public @responsebody String UploadNewModel(@RequestParam("file") MultipartFile multipartFile){
try {
System.out.println("THE FILE " + multipartFile.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
File file = null;
try {

        file = new File(multipartFile.getOriginalFilename());
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(multipartFile.getBytes());
        fos.close();
    }catch (Exception ex){
        log.error("ERROR Creating FIle from MultiPartFile: " + ex.getMessage());
    }

....
}

@LFJustin LFJustin reopened this Sep 26, 2014
@danialfarid
Copy link
Owner

Is there any more information on the Bad Request error?

@danialfarid
Copy link
Owner

You can try to get a regular file form with submit button plain html to work with Spring. Once you have that working then it should work with the plugin as well. There should be plenty of tutorial on regular form submit file upload with Spring.

@LFJustin
Copy link
Author

I got a regular form to work on my localhost but once I push it to my test server I keep getting a bad request error. "There was an unexpected error (type=Bad Request, status=400).
Required MultipartFile parameter 'file' is not present"

@danialfarid
Copy link
Owner

Well getting the regular form working on your server would be the first step and is not related to this plugin, You can ask in the Spring forum or on stackoverflow.

@LFJustin LFJustin closed this as completed Oct 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants