使用ajax将图像发送到PLAY框架

时间:2014-03-26 09:49:54

标签: jquery ajax image playframework

我尝试使用AJAX将图像发送到Java PlayFramework,然后将其保存到服务器中的某个位置。

HTML代码仅限:

<input type="file" accept="image/*" capture="camera" id="image">

那么,如何使用AJAX发送文件? 以及如何使用PLAY Framework获取文件?

我已经使用了base64字符串,但是获得异常说&#34;字符串太长&#34;。 尺寸较小的图像没有问题。 但是,数百KB或更高的图像将获得例外。

任何解决方案或更好的方法吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用here

之类的Formdata
var fd = new FormData();    
fd.append( 'file', input.files[0] );

$.ajax({
  url: 'http://example.com/script.php',
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }
});

答案 1 :(得分:0)

也许这可以帮到你。 这实际上是如何通过表单发送和存储mp3(base64)到PLAY Framework。我希望这可以帮助你一点:

<强> main.scala.html

<!-- AUDIO -->
    <section id="sound">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
    <form action="**@routes.Application.upload()**"  name="addProductForm" id="addProductForm" enctype="multipart/form-data" method="post">
        <label>Select file.
            <input name="base64" type="text" accept="*/*" value="data:audio/mp3;base64,//sQxAAABKh...JZsdf2y/FOtQ==" >
            <!-- START This is actually not needed.Because you dont want use the uploader itself -->
            <input name="picture" type="file" accept="*/*" >
            <!-- ENDE This is actually not needed.Because you dont want use the uploader itself -->
          </label>
        <input name="submit" type="submit" size="50" value="senden">

    </form>
                </div>
            </div>
        </div>
    </section>
    <!-- AUDIO -->

非常重要,要在Java中使用FileUtils,只需安装org.apache.common.io.FileUtils(!!!)

<强> Application.java

public static Result upload(){

    System.out.println("Entered the upload() method !!!");

    play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();
    DynamicForm dynamicForm = Form.form().bindFromRequest();

    play.mvc.Http.MultipartFormData.FilePart picture = body.getFile("picture");
    String str = dynamicForm.get("base64");

    // example: "data:audio/mp3;base64,//sQxAAABKhrG...f2y/FOtQ==";

    String[] strsplited = str.split(",");
    String b64 = strsplited[1];
    if (picture != null) {

        File file = picture.getFile();
        String fileName = file.getName();

        System.out.println("\n\n"+file.getPath()+"/"+fileName);
        try {
            /* START This is actually not needed.Because you dont want use the uploader itself */
            File destination = new File("/Applications/MAMP/htdocs/play_old/myapp/public/upload/", file.getName());
            FileUtils.moveFile(file,destination);
            /* ENDE This is actually not needed.Because you dont want use the uploader itself */

            java.security.SecureRandom random = new java.security.SecureRandom();
            String newFileName = new BigInteger(130, random).toString(32);
            byte[] data = Base64.getDecoder().decode(b64);
            File dest = new File("/Applications/MAMP/htdocs/play_old/myapp/public/upload/", newFileName+".mp3");
            FileUtils.writeByteArrayToFile(dest,data);
            System.out.println("Byte size: "+data.length);
            System.out.println("Create new mp3 file :"+bi+".mp3");

        } catch(IOException e){
            e.getMessage();
        }

    } else {
        flash("error", "Missing file");
        return badRequest();
    }

    return ok("File uploaded");

}

<强>路由

POST  /upload           controllers.Application.upload()