2012-06-03 60 views
2

如何從MultipartFormData獲取DataPart?我找不到任何API來獲取它。播放2如何從MultipartFormData獲取DataPart

Http.MultipartFormData formData = body.asMultipartFormData(); 

// simple form field 
// there is NO getData() or something available 
DataPart imageIdPart = formData.getData("dataKey"); 

// uploaded file 
FilePart imagePart = formData.getFile("imageKey"); 

回答

12

我不習慣在Play 2.0中使用Java,但是像這樣工作嗎?

@BodyParser.Of(BodyParser.MultipartFormData.class) 
public static Result index() { 
    Http.MultipartFormData multipartFormData = request().body().asMultipartFormData(); 

    //ask the multipart to be form url encoded... 
    //and get the data 
    String[] data = multipartFormData.asFormUrlEncoded().get("dataKey"); 

    //which should not impact such call 
    Http.MultipartFormData.FilePart image = multipartFormData.getFile("imageKey"); 

    return ok("Got image: " + image.getFilename()); 
} 
+0

非常感謝您! – sealskej

5

斯卡拉:

def index = Action(parse.multipartFormData) { 
    request => { 

    val dataKey = request.body.dataParts.get("dataKey") 
    something something... 

    Ok("Ok!") 
} 
相關問題