Question

I'm trying to check the size of a file uploaded in a Play application (2.0.3).

I tried both Scala and Java controllers, but I've got the same weird behaviour every time ... I manage to detect that the file is too large, but when I try to return a response, the request hangs forever and the user is not aware that the request is not valid.

In Java :

@BodyParser.Of(value = BodyParser.MultipartFormData.class, maxLength = 10 * 1024 * 1024)
pulic static Result upload() {
  if(request().body().isMaxSizeExceeded()) {
    return badRequest("Too much data!"); // this is not returned
  } else {
    ok("Got body: " + request().body().asText()); 
  }
}

In Scala :

def upload = Action(parse.maxLength(10 * 1024 * 1024, parse.multipartFormData)) { request =>
    request.body match {
        case Left(MaxSizeExceeded(length)) => {
            Logger.error("MaxSizeExceeded")
            BadRequest("Your file is too large, we accept just " + length + " bytes!")
        }
        case Right(multipartForm) => {
            // Do stuff to handle the file
        }
    }
}

The HTML template :

@(httpPath: java.lang.String) @main(httpPath) {

    @helper.form(action = routes.Application.upload(), 'enctype -> "multipart/form-data", 'class -> "form-horizontal", 'id -> "form") {

        <div id="well" class="well">

                <h1>Upload</h1>
                <div id="formGroup" class="control-group">
                    <label class="control-label" for="file">Select file : </label>
                    <div class="controls">
                        <input id="file" name="file" type="file" style="display: none" /> 

                        <div class="input-append">
                            <input id="txtFile" type="text" class="required" readonly="true"/>
                            <span class="btn" onclick="$('#file').click();">Browse</span>
                        </div>

                        <script type="text/javascript">
                             $('#file').change(function() { 
                                 $('#txtFile').val($(this).val()); 
                             }); 
                         </script>
                    </div>
                </div>

                <button id="submit" type="submit" class="btn btn-primary" >Upload</button>
        </div>
    }
}

Log :

2014-04-30 16:46:44,791 - [[trace]] - play - New I/O  worker #4 - Serving this request with: Action(parser=BodyParser(maxLength=1048576, wrapping=BodyParser(multipartFormData))) -
2014-04-30 16:46:44,996 - [[trace]] - play - play-akka.actor.promises-dispatcher-60 - Invoking action with request: POST /upload -
2014-04-30 16:46:44,998 - [[error]] - application - play-akka.actor.actions-dispatcher-10 - MaxSizeExceeded java -
2014-04-30 16:46:44,999 - [[trace]] - play - play-akka.actor.actions-dispatcher-10 - Sending simple result: SimpleResult(400, Map(Content-Type -> text/plain; charset=utf-8, Set-Cookie -> )) -

I saw that other developers faced the same problem (eg https://groups.google.com/forum/#!topic/play-framework/wuXnoXN5GZ0). Is this a known issue in 2.0.x? Am I doing something wrong?

Thanks

Was it helpful?

Solution

By upgrading Play version to 2.2.1, the problem disappears.

I tried with the last 2.0.x version published (2.0.8), the bug is still present.

Apparently, it has been fixed in 2.1.x.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top