troubleshooting Question

File upload

Avatar of tonelm54
tonelm54 asked on
JavaScriptWeb DevelopmentjQuery
5 Comments1 Solution76 ViewsLast Modified:
Ive been trying to figure out how to do my own file upload from a dropped file. Ive cobbled together from multiple articles and got the following:-
<!DOCTYPE html>
<html lang="en">
<head>
<script src="jquery-3.4.1.js"></script>

<style>
#testDiv {
    height:150px;
    width:150px;
    border: 1px solid black;
}
</style>

</head>
<body>
<div id="testDiv">nnn</div>
</body>
</html>

<script>
$(document).ready(function() {
$('#testDiv').on('dragover', function(e) {
        e.preventDefault();
        e.stopPropagation();
    });

$('#testDiv').on('dragenter', function(e) {
        e.preventDefault();
        e.stopPropagation();
    });

$('#testDiv').on('drop', function(e){
    if(e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files.length) {
        e.preventDefault();
        e.stopPropagation();
        myOwnCallBack(e.originalEvent.dataTransfer.files);
        }
    });
});

function myOwnCallBack(file) {
    let data = new FormData();

    for(var i = 0;i<file.length;i++){
        data.append("file_"+i, file[i]);
    }
    data.append("test1", "value1");
    data.append("test2", "value2");

    $.ajax({
        data: data,
        type: "POST",
        url: "file-uploader.php",
        cache: false,
        contentType: false,
        processData: false,
        xhr: function() {
            let myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
            return myXhr;
        },
        success: function(reponse) {
        alert("uploaded");
        }
    });
}

function progressHandlingFunction(e) {
    if (e.lengthComputable) {
        console.log((e.loaded / e.total * 100) + '%');

        if (e.loaded === e.total) {
            console.log("Upload finished.");
        }
    }
}
</script>

My test values transfer fine, but the file(s) dont transfer.

Can anyone see what Im doing wrong with the files?

Thank you in advance
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros