raphidue
raphidue

Reputation: 141

jQuery-File-Upload with Java Play Framework NetworkError: 500

im using Java Play Framework and this File-Upload-Plugin: http://blueimp.github.io/jQuery-File-Upload/ . After read the documentation http://www.playframework.com/documentation/2.0/JavaFileUpload i used this specific code in my Java Play Controller.

public static Result upload() {
  File file = request().body().asRaw().asFile();
  return ok("File uploaded");
}

I've also added this route to my project:

POST    /upload                     controllers.Image.upload()

My View-Template:

@(scripts: Html)

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="/upload">
@scripts
<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.context = $('<p/>').text('Uploading...').appendTo(document.body);
            data.submit();
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});
</script>
</body> 

Now if i upload a image firebug shows me the following error:

"NetworkError: 500 Internal Server Error - http://localhost:9000/upload"

The error was caused by this one line in the Controller Upload-action:

  File file = request().body().asRaw().asFile();

Anyone know a solution? Thank you for your help.

Upvotes: 4

Views: 1884

Answers (2)

Mohan Rex
Mohan Rex

Reputation: 1

Actually this is my controller part by which i can move the file from my temp folder to the application public folder so that i can use it further. the json will be returned as a multipart form data. so we have to use like this. Hope this solves your problem.

MultipartFormData body = request().body().asMultipartFormData();
        FilePart picture = body.getFile("file");
            if (picture != null) {
                File tempimg = picture.getFile();
                Path temp = tempimg.toPath();
                Path newFile = new File(Play.application().path().getAbsolutePath()+"/public/uploaded",picture.getFilename()).toPath();
                Files.move(temp, newFile);
                return ok("File uploaded");
            } else {
                flash("error", "Missing file");
                return badRequest("File Missing");    
            }

Upvotes: 0

nadouani
nadouani

Reputation: 115

I guess you can access your upload files differently. You can use something like:

    Http.MultipartFormData body = request().body().asMultipartFormData();

    for(Http.MultipartFormData.FilePart part : body.getFiles()){
        Logger.debug(part.getFilename());
        Logger.debug(part.getKey());
        Logger.debug(part.getContentType());
        Logger.debug(part.getFile().getName());
        Logger.debug(part.getFile().getAbsolutePath());
        Logger.debug(String.valueOf(part.getFile().getTotalSpace()));
    }

One you have your java.io.File instance you can do whatever you want

Upvotes: 1

Related Questions