首页 文章

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

提问于
浏览
0

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

HTML代码仅限:

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

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

我已经使用了base64字符串,但获得异常称“字符串太长” . 尺寸较小的图像没有问题 . 但是数百KB或更高的图像将获得异常 .

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

谢谢 .

2 回答

  • 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);
      }
    });
    
  • 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");
    
    }
    

    routes

    POST  /upload           controllers.Application.upload()
    

相关问题