1

JSP Form :

    <s:form method="POST" enctype="multipart/form-data" action="uploaddocumentfile" id="uploaddocumentfile" namespace="/documents" >
        <s:file name="upload" id="holder"></s:file>
   /s:form>

jQuery:

function readfiles(files) {
    console.log(files);
    var a=files[0].name;  // name of file 
    $("#uploaddocumentfile").submit(); // problem :how to pass value in action class of type File upload  getter setter
}
var holder = document.getElementById('holder');
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
     this.className = '';
    // console.log(e.dataTransfer.files[0].path);
     e.preventDefault();
     readfiles(e.dataTransfer.files);
    } 

Action class: public class UploadFilesAction extends ... {

private File upload;

public File getUpload() {
    return upload;
}
public void setUpload(File upload) {
    this.upload = upload;
}
}

problem : in browse file upload we can get form file value in action class automatically by File upload getter setter but drag drop how can we get file values

kamlesh
  • 139
  • 2
  • 13
  • file upload example in struts : https://www.javatpoint.com/struts-2-file-upload-example same type need to drag and drop file uploads – kamlesh May 09 '17 at 07:24
  • The drag and drop on that example is working fine.. https://www.javatpoint.com/struts-2-file-upload-example – Prince Radhakrishnan May 09 '17 at 09:03
  • this is not drag and drop , java point example is browse image file examle .. drag drop means from computer drag a file and drop in webpage specific area ..http://html5demos.com/dnd-upload – kamlesh May 09 '17 at 09:25
  • Try drag files to choose file button it can be upload.If you want a div to act like a drag and drop for file you need to use jquery plugins like http://www.dropzonejs.com/ – Prince Radhakrishnan May 09 '17 at 09:35

1 Answers1

0

You can use dropzonejs plugin to achieve drag and drop files

JSP

<div id="template_dropzone"></div>

Script

var profilePic = null;
var dropZone = $('#template_dropzone').dropzone({
                 url: "#",
                 maxFiles: 1,
                 addRemoveLinks: true,
                 init: function() {
                    this.on("addedfile", function(file) {
                     // trigger when a file is added
                 });
                     this.on("complete", function(file) {
                       // trigger when the uploading of a file is completed
                        profilePic = file;
                  });
                     this.on("removedfile", function(file) {
                         var noOfFiles =  this.files.length; // can check No. of files
                  });
               });
    $('#uploadFile').on('click',function(event){
      var data = new FormData();
      data.append('upload',profilePic);
       $.ajax({
           url: 'uploadProfilePic',
           method: 'POST',
           data: data,
           cache: false,
           contentType: false,
           processData: false,
           success: function(result) {}
        });
    });

In Action class you can retrieve file name or content type by simply creating a String variable with getters and setters like below:-

    private File upload;
    private String uploadFileName;
    private String uploadContentType;

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadFileContentType() {
        return uploadFileContentType;
    }

    public void setUploadFileContentType(String uploadFileContentType) {
        this.uploadFileContentType = uploadFileContentType;
    }
  • i do not want to use any plugin.. can i pass profilePic value as a value in my code and insted of calling onclick listner can call directly in read file.. function readfiles(files) { console.log(files); var a=files[0].name; // name of file var data = new FormData(); data.append('upload',a); $.ajax({ url: 'uploadProfilePic', method: 'POST', data: data, cache: false, contentType: false, processData: false, success: function(result) {} }); ...... – kamlesh May 09 '17 at 11:20
  • if using dropzone js then jsp file will
    – kamlesh May 09 '17 at 11:29
  • Yes you can. make sure `a` is file not file name `var a=files[0];' – Prince Radhakrishnan May 09 '17 at 11:30
  • The request is send via ajax not form submit. – Prince Radhakrishnan May 09 '17 at 11:36
  • can you give me exact jsp file – kamlesh May 09 '17 at 11:37
  • If you want to upload file the moment it is dropped into the div.Use ajax inside `this.on("complete", function(file) {}` else you can upload it with a button click,like in the example.No need to create form in html.Add form action to ajax url parameter. – Prince Radhakrishnan May 09 '17 at 11:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143774/discussion-between-prince-radhakrishnan-and-kaml). – Prince Radhakrishnan May 09 '17 at 11:42
  • 1
    It is worth noting that, to upload multiple files with Struts2 and dropzone.js, a custom hack is needed. Luckily, [I've documented it here](http://stackoverflow.com/a/26080212/1654265) ;) – Andrea Ligios May 09 '17 at 14:04