2013-07-10 33 views
4

我有一个Windows Phone 8的客户端,使下面的POST请求请求它自动作为“multipart/form-data”请求。多文件上传和JSON对象在游戏框架一个POST请求

在玩2.1的上传操作的控制器如下:

@BodyParser.Of(BodyParser.Json.class) 
public static Result createMessage() { 
    JsonNode json = request().body().asJson(); 
    ObjectNode result = Json.newObject(); 
    String userId = json.findPath("userId").getTextValue(); 
    String rayz = json.findPath("message").getTextValue(); 

    Http.MultipartFormData body = request().body().asMultipartFormData(); 
    Http.MultipartFormData.FilePart picture = body.getFile("picture"); 

    if (picture != null) { 
     String fileName = picture.getFilename(); 
     String contentType = picture.getContentType(); 
     File file = picture.getFile(); 

     result.put("status", "success"); 
     result.put("message", "Created message!"); 
     return badRequest(result); 
    } else { 
     result.put("status", "error"); 
     result.put("message", "Message cannot be created!"); 
     return badRequest(result); 
    } 
} 

注意,在application.conf我已成立,以增加大小限制(似乎不工作)以下内容:现在

# Application settings 
# ~~~~~ 
parsers.text.maxLength=102400K 

,每次我试图使POST请求我注意到对IsMaxSizeEsceeded变量调试时间总是真实的多变量为空。当我试图使用下面的控制器上传一个文件nu时,一切似乎都正常工作。大小不是问题,并设置了多部分变量。

public static Result singleUpload() { 
    ObjectNode result = Json.newObject(); 

    Http.MultipartFormData body = request().body().asMultipartFormData(); 
    Http.MultipartFormData.FilePart picture = body.getFile("picture"); 
    if (picture != null) { 
     String fileName = picture.getFilename(); 
     String contentType = picture.getContentType(); 
     File file = picture.getFile(); 
     result.put("status", "success"); 
     result.put("message", "File uploaded!"); 
     return badRequest(result); 
    } else { 
     result.put("status", "error"); 
     result.put("message", "File cannot be uploaded!"); 
     return badRequest(result); 
    } 
} 

的问题是,附件/文件应该被发送/与JSON对象到服务器沿着在单个POST请求上传和未单独。

有没有人遇到过类似的问题?是否有可能实现这一点 - 发送一个json对象和多个文件上传到服务器在一个POST请求与Play 2.1?

回答

8

找到办法做到这一点.. ..如果有人试图在未来做类似的事情。

所以首先从RestSharp客户的所有要求必须按以下方式进行的:

public async Task<string> DoPostRequestWithAttachmentsAsync(String ext, JSonWriter jsonObject, ObservableCollection<byte[]> attachments) { 
    var client = new RestClient(DefaultUri); 
    var request = new RestRequest(ext, Method.POST); 

    request.RequestFormat = DataFormat.Json; 
    request.AddParameter("json", jsonObject.ToString(), ParameterType.GetOrPost); 

    // add files to upload 
    foreach (var a in attachments) 
     request.AddFile("attachment", a, "someFileName"); 

    var content = await client.GetResponseAsync(request); 

    if (content.StatusCode != HttpStatusCode.OK) 
      return <error>; 

    return content.Content; 
} 

现在移动到播放控制器如下:

public static Result createMessage() { 
    List<Http.MultipartFormData.FilePart> attachments; 
    String json_str; 

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

    // If the body is multipart get the json object asMultipartFormData() 
    if (body!=null) { 
     json_str = request().body().asMultipartFormData().asFormUrlEncoded().get("json")[0]; 
     attachments= body.getFiles(); 
    } 
    // Else, if the body is not multipart get the json object asFormUrlEncoded() 
    else { 
     json_str = request().body().asFormUrlEncoded().get("json")[0]; 
     attachments = Collections.emptyList(); 
    } 

    // Parse the Json Object 
    JsonNode json = Json.parse(json_str); 

    // Iterate through the uploaded files and save them to the server 
    for (Http.MultipartFormData.FilePart o : attachments) 
     FileManager.SaveAttachmentToServer(o.getContentType(), o.getFile()); 

    return ok(); 
} 

所以在失误RestSharp方面似乎是json对象上的ParameterType.RequestBody;并在控制器中的大小并没有真正改变任何东西..但重要的是不使用@ BodyParser.Of(BodyParser.Json.class),因为它会将整个请求体转换成json对象。这与正在发送到服务器的文件结合触发isMaxSizeExceeded标志。

最后,控制器中的多部分文件处理如上所示,唯一棘手的部分是附件是可选的,这意味着必须处理。

相关问题