2014-03-26 72 views
0

我想使用AJAX將圖像發送到Java PlayFramework,然後將其保存到服務器中的某處。用ajax發送圖像到PLAY框架

的HTML代碼只是:

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

那麼,如何使用AJAX來發送文件? 以及如何使用PLAY框架獲取文件?

我已經使用了base64字符串,但得到異常說「字符串太長」。 尺寸較小的圖像沒有問題。 但數百KB或以上的圖像將會得到例外。

任何解決方案或更好的方法呢?

謝謝。

回答

0

您可以使用FORMDATA像here

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框架。我希望這能幫助你一點:

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中只需安裝org.apache.common.io.FileUtils(!!!)

應用。 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()