我正在疯狂地尝试使用 scribe、play 框架和 jquery mobile 为 twitter 上传一个简单的文件。
请,任何人都可以帮助设置正确的参数以通过 $.ajax 发送文件来播放框架?
控制器方法:
public static Result postMediaJson() {
Logger.info("**** postMediaJson ****");
Logger.info("uid=" + uid);
if (uid == null) {
ObjectNode result = Json.newObject();
result.put("accessTokenNull", "1");
Logger.info("8.accessTokenNull");
return ok(result);
}
Token accessToken = (Token) Cache.get(uid + "accessToken");
Logger.info("accessToken=" + accessToken);
if (accessToken == null) {
ObjectNode result = Json.newObject();
result.put("accessTokenNull", "1");
Logger.info("9.accessTokenNull");
return ok(result);
}
OAuthRequest request = new OAuthRequest(Verb.POST, UPDATEMEDIA_RESOURCE_URL);
service.signRequest(accessToken, request);
Logger.info("service=" + service);
try
{
String vparam = form().bindFromRequest().get("status");
Logger.info("vparam=" + vparam);
MultipartFormData body = request().body().asMultipartFormData();
FilePart resourceFile = body.getFile("picture");
if (resourceFile != null) {
String fileName = resourceFile.getFilename();
String contentType = resourceFile.getContentType();
File file = resourceFile.getFile();
Logger.info("Justo antes");
if (file != null && vparam != null) {
Logger.info("Entra");
// added lines
//String myUploadPath = Play.application().configuration().getString("myUploadPath");
//file.renameTo(new File(myUploadPath, fileName));
MultipartEntity entity = new MultipartEntity();
Logger.info("1Entra");
entity.addPart("status", new StringBody(vparam)); // THIS IS THE TWITTER MESSAGE
Logger.info("2Entra");
entity.addPart("media", new FileBody(file)); // THIS IS THE PHOTO TO UPLOAD
Logger.info("3Entra");
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
Logger.info("4Entra");
request.addPayload(out.toByteArray());
Logger.info("5Entra");
request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
Logger.info("6Entra");
}else{
ObjectNode result = Json.newObject();
result.put("accessTokenNull", "1");
Logger.info("10.accessTokenNull");
return ok(result);
}
}else{
ObjectNode result = Json.newObject();
result.put("accessTokenNull", "1");
Logger.info("11.accessTokenNull");
return ok(result);
}
}catch (UnsupportedEncodingException e) {
Logger.info("UnsupportedEncodingException");
e.printStackTrace();
}catch (IOException e) {
Logger.info("IOException");
e.printStackTrace();
}catch (NullPointerException e) {
Logger.info("NullPointerException");
e.printStackTrace();
ObjectNode result = Json.newObject();
result.put("accessTokenNull", "1");
Logger.info("12.accessTokenNull");
return ok(result);
}
Response response = request.send();
if ( 0L == Analytic.incPokes() )
Logger.info("Creating first poke with media of today.");
return ok(Json.toJson(response.getBody()));
}
网页:
<div data-role="header" data-theme="a">
<h1>Header</h1>
<a href="#pageDashboard" data-transition="slide" data-role="button" data-theme="a" data-mini="true" data-direction="reverse">Volver</a>
</div>
<div data-role="content">
<form id="frmPostMedia" method="POST" enctype="multipart/form-data" action="@routes.Application.postMediaJson()" data-ajax="false">
<label for="taMPost">¿Qué está pasando?:</label>
<textarea cols="40" rows="7" maxlength="140" id="status" name="status"></textarea>
<div id="lblMCount"></div>
<input type="file" id="picture" name="picture" />
<input type="submit" data-transition="slide" data-theme="a" data-mini="true" value="Enviar" />
</form>
</div>
和咖啡脚本:
$("#pagePostMedia").on 'pageinit', (event) ->
$("#frmPostMedia").on 'submit', (event) ->
event.preventDefault()
return alert "No puedes pedir apoyo con un mensaje en blanco. ¡Intenta escribir un mensaje inspirador!"if $("#status").val().length < 3
$("#frmPostMedia").attr("enctype", "multipart/form-data")
$("#frmPostMedia").attr("encoding", "multipart/form-data")
formData = new FormData($(this)[0])
$.ajax '/postMediaJson',
type: 'POST'
data: formData
contentType: 'multipart/form-data'
processData: false
cache : false
async: false
success: (data)->
alert JSON.stringify(data)
error: (data)->
alert "ERROR="+ JSON.stringify(data)
$("#pagePostMedia").on 'pageshow', (event) ->
res = 140 - $("#status").val().length
$("#lblMCount").html("Te quedan <strong>" + res + "</strong> caracteres.")
$("#status").on 'keyup', (event) ->
res = 140 - $("#status").val().length
$("#lblMCount").html("Te quedan <strong>" + res + "</strong> caracteres.")
结果 request().body().asMultipartFormData() 始终为 null :-(