0

I have problem with uploading files in my existing form. What I am looking for is script that will make possible to add multiple files (max 5) and you can add at once from one to five files. If you add one by one, I need it to add new, not replace the previous one.

I got form looking like this:

Name
LastName
Email
Phone number
Interests
Files

and filenames are created like this: name+lastname+phonenumber+filename

And I add entry to database with path of everyfile - this is done and I need only good drag and drop zone.

I need it to show added filename and make it possible to delete added file from queue.

But I don't want files to upload when I add them. I want it to upload when I submit my whole form so filename can be created and path to DB can be added.

Could anyone please provide me good script to that, or based on my scripts from two topics I mentioned before make it avaiable to do what I want?

I was able to add 5 files one by one and I described it here: HTML Add multiple file to the input

Also I was able to add more at once what I described here: https://stackoverflow.com/questions/30499388/dropzone-js-into-another-form

Community
  • 1
  • 1
arienn
  • 230
  • 6
  • 18

1 Answers1

2

I think that this example help you. This app allow drag and drop files to gray zone (1 or 5) If you click on the file name, it removes file from the list.

function init() {
    //get dragdrop element
    var dd = document.getElementById("dragdrop");
    //get files element
    $files = document.getElementById("files");
    dd.ondragover = stop;
    dd.ondragleave = stop;
    if ('FileReader' in window) {
        document.ondrop = dragAccept;
    }

    //get form
    var $form = document.querySelector("form");
    //catch on submit
    $form.onsubmit = function (e) {
        stop(e);
        var fd = new FormData();
        //apend files to FormData
        for (var i in files){
            var file = files[i].file;
            var filename = file.name;
            var name = "file";
            fd.append(name, file, filename);
        };
        //append inputs to FormData
        var $inputs = $form.querySelectorAll("input");
        for (var i = 0; i < $inputs.length; i++) {
            var $input = $inputs[i];
            fd.append($input.getAttribute("name"), $input.value);
        }
        //Send data
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/echo/html/', true);
        xhr.send(fd)
    }
}

function stop(e) {
    e.stopPropagation();
    e.preventDefault();
}

function dragAccept(e) {
    stop(e);
    if (e.dataTransfer.files.length > 0)
        for (var i = 0; i < e.dataTransfer.files.length; i++) {
            addFile(e.dataTransfer.files[i]);
        }
}

//file list store
var files = {};
// html element of file list
var $files = null;
//add file to file list
function addFile(file) {
    //add files with diferent name, max files count 5
    if (!(file.name in files) && Object.keys(files).length < 5) {
        var div = createFile(file.name);
        $files.appendChild(div);
        files[file.name] = {
            file: file,
            element: div
        }
    }
}

//create html element with file name
function createFile(name) {
    var div = document.createElement("div");
    div.innerText = name;
    var input = document.createElement("input")
    //remove on click
    div.addEventListener("click", function () {
        $files.removeChild(this);
        delete files[name];
    })
    return div;
}


window.addEventListener("load", init);
<form method="post" enctype="multipart/form-data" action="">
    <label>Name<input name="name" /></label>
    <label>Last name<input name="lastName" /></label>
    <label>Email<input name="email" /></label>
    <div id="dragdrop" style="width: 300px; height: 300px; background-color:lightgray">Drag drop zone</div>
    <div id="files"></div>
    <button type="submit">Send</button>
</form>
Microshine
  • 731
  • 5
  • 19
  • It works for me but the problem is, when I submit my form the files aren't submitted with it. How can I send them as files added to regular file input? – arienn May 30 '15 at 21:18
  • I am not really good in JS, I have to add this functionality but I can't figure out how – arienn May 30 '15 at 21:29
  • @arienn See this [post](http://stackoverflow.com/questions/1017224/dynamically-set-value-of-a-file-input). I think that it will be helpfull – Microshine May 30 '15 at 21:29
  • I copy/pasted your code, but files I add to this dropzone aren't send with my form on submit and I cant figure out why. When I use code I provided they are send, with yours, everything works great, but files aren't send and my servlet has nothing to catch and save :( – arienn May 30 '15 at 22:14
  • I found this sample of dynamic adding of files to form. [Sample](http://stackoverflow.com/questions/21901528/how-to-append-multiple-file-inputs-to-a-formdata-object-using-each) – Microshine May 30 '15 at 22:22
  • `var inputs = $("files"); $.each(inputs, function (obj, v) { var file = v.files[0]; var filename = $(v).attr("data-filename"); var name = $(v).attr("id"); dataForm.append(name, file, filename); }); var xhr = new XMLHttpRequest; xhr.open('POST', '/echo/html/', true); xhr.send(dataForm);` is this correct way for example you provided? – arienn May 30 '15 at 22:56
  • I edited my code. When you click send function creates FormData and fills it. Then you can send FormData via XMLHttpRequest – Microshine May 31 '15 at 12:11
  • Still files seems not to be uploaded, servlet don't catch them – arienn May 31 '15 at 20:03
  • I found a bit information about drag and drop files. Read this [article](http://www.nczonline.net/blog/2012/05/08/working-with-files-in-javascript-part-1/) – Microshine Jun 01 '15 at 05:44