1

I have one scenario: I am uploading one file at some server location using: https://www.playframework.com/documentation/2.0/JavaFileUpload ,

//Uploading file:

<input type="file" name="fileUpload">
<input type="submit" value="Upload">

And from the below code, I am uploading the above uploaded file and getting/displaying it on my view page like(After clicking the Submit button):

<input type="file" id="inputfile">
<input type="button" value="Submit" id="submitfile">

jQuery:

$("#submitfile").click(function(){
    var path1 =$('input[type=file]').val().replace(/C:\\fakepath\\/i, '');//uploaded file names 
    //adding the Play framework server path for Application to get the image(s) file(s) path
    var filespath = '/files/images/'+path1;//giving my uploaded files path here

    });

But my requirement is that: I need only one type which does both: accepts/uploads the file at server location and returns/displays the same file path from server location on my view page ? I am struggling for it. Please help me.

Dhana
  • 711
  • 3
  • 14
  • 40

1 Answers1

3

This looks like a similar issue to 33163555. That question has the following example:

Edit: Reference 2320069 for support on ajax file uploads & alternatives:

FormData support starts from following desktop browsers versions. IE 10+, Firefox 4.0+, Chrome 7+, Safari 5+, Opera 12+

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<form enctype="multipart/form-data">
  <input type="file" id="file" name="file" />
  <input type="submit" id="submit" name="" value="Upload" />
</form>

<script>
$('#submit').click(function (event) {
   event.preventDefault();
   var file = $('#file').get(0).files[0];
   var formData = new FormData();
   formData.append('file', file);
   $.ajax({
       url: 'upload',
       data: formData,
       type: 'POST',
       contentType: false,
       processData: false,
       beforeSend: function (data) {
         alert('Are you sure you want to upload document?');
       },
       success: function (data) {
         //call your jQuery action here
         alert('Upload completed: ' + data);
       },
       error: function (jqXHR, textStatus, errorThrown) {
         alert(textStatus + ': ' + errorThrown);
       }
    });
    return false;
});
</script>

In your routes you have:

POST /upload controllers.Application.upload()

Where your controller method returns the filepath:

public static Result upload() {
  MultipartFormData body = request().body().asMultipartFormData();
  FilePart fileP = body.getFile("file");
  if (fileP != null) {
    File file = fileP.getFile();
    //If we want to move from temp
    //FileUtils.moveFile(file.getCanonicalPath(), "FileB"); 
    return ok(file.getCanonicalPath());
  } else {
    return badRequest("Upload Error");    
  }
}

And you can perform your custom jQuery action in the ajax success callback

Community
  • 1
  • 1
Chrizt0f
  • 314
  • 1
  • 12
  • I am getting Internal Server Error – Dhana Oct 20 '15 at 08:56
  • Can we implement it above like : http://stackoverflow.com/questions/33209460/how-to-redirect-to-same-overlay-on-return-statement-in-play-framework – Dhana Oct 20 '15 at 08:58
  • 1
    Oh sorry haha. I didn't realize you posted my referenced question. Editing my answer based on [2320069](http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) , [20452853](http://stackoverflow.com/questions/20452853/uploading-files-via-jquery-object-formdata-is-provided-and-no-file-name-get-re), [6974684](http://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery) which say that you need to store your file into an ajax formData object before sending, or take another approach – Chrizt0f Oct 20 '15 at 14:30
  • 1
    Excellent, It's working great !! Hat's off !! You saved me !! – Dhana Oct 21 '15 at 05:54
  • I am getting internal server error for above ajax call – dnvsp Aug 17 '16 at 08:46