Re: [1.2] Submit file with Ajax... help please

79 views
Skip to first unread message

christian sarnataro

unread,
Jun 27, 2012, 8:31:28 AM6/27/12
to play-fr...@googlegroups.com
Just a hint:
try to set 

    contentType: 'multipart/form-data'

in ajax request.

See:
http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax



On Tuesday, June 26, 2012 9:36:30 PM UTC+2, inforux wrote:
Good  morning.
thanks for read me. I'm trying to send a form with an input type file, and then preview it. alll with ajax or  thing similar
-------------------------------
Load.html

#{form @Application.load(), enctype:'multipart/form-data'}

<input name="file" id="inputFileSF" onchange="preLoad(this.form)" type="file">
#{/form}
       <div id="divPreview"> </div>
-------------------------------
Preview.html
        <img style="width:180px;max-height:auto;" src="@{Application.preview()}">
-------------------------------
JavaScripth:
        
      function preLoad(frm)
       {
                frm.submit( function() {
                        $.ajax({
                        type: 'POST',
                        url: '@Application.load()',
                         data: $(this).serialize(),
                
                         success: function(data) {
                         alert("workin ok")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                          } 
                                })      
                                return false;
                         } );
                        /*update the div*/
                       var actionui = #{jsAction @Application.preview()/}
$("#divPreview").load(actionui({}), function(){ })

     }
-------------------------------
Application.java
        public static File tmpFile;
        public static void load(File file) {
if (file != null){
                System.out.println("goooo ok" );
tmpFile = file;
}
else System.out.println("null");
}
 
       public static preview(){
               renderBinary(tmpFile);
      }

------------------------------------------


But no working.
What am I doing wrong?


pd: sorry for the english... but i'm from peru.
thanks







Marcel Klemenz

unread,
Jun 28, 2012, 8:06:09 AM6/28/12
to play-fr...@googlegroups.com
what is not working? The upload or the preview?
If your preview is the non working part, you have to set
the contentType for this response:
Response.current().contentType = "image/png";// or jpg

For multiusersupport you have to get rid of your static tmpfile variable.
other users will use THIS variable too, so they change the content.

Wonder if the controller would even work with a single user?
Store an UUID and the file-path in cache. use the same UUID in preview
to get the file back.

greets,
maklemenz
Reply all
Reply to author
Forward
0 new messages