0

I am able to drag and drop a file and then send the data to a server side Application via RESTful interface.

But, I want to validate the file on the client side before the data goes all the way...

I know there are something called .accept for the droppeable but I couldn't make it work.

I'll be happy if only could check if the file has a .CSV extension, but I could also touch the sky if I could check if the file is a real .CSV file

here my code:

<script>
    $(document).ready(function () {

        $("#progress").hide();

        $("#fileBasket").on("dragenter", function (evt) {
            evt.preventDefault();
            evt.stopPropagation();
        });

        $("#fileBasket").on("dragover", function (evt) {
            evt.preventDefault();
            evt.stopPropagation();
        });

        $("#fileBasket").on("drop", function (evt) {
            evt.preventDefault();
            evt.stopPropagation();

            });
    });

    $("#fileBasket").on("drop", function (evt) {
        evt.preventDefault();
        evt.stopPropagation();
        var files = evt.originalEvent.dataTransfer.files;
        var fileNames = "";

        if (files.length > 0 && files.length <2) {
            fileNames += "Uploading <br/>"
            for (var i = 0; i < files.length; i++) {
                fileNames += files[i].name + "<br />";
            }
        }


        $("#fileBasket").html(fileNames)

        var data = new FormData();
        for (var i = 0; i < files.length; i++) {
            data.append(files[i].name, files[i]);
        }



        $.ajax({
            type: "POST",
            url: "http://localhost:25516/api/Sales/uploadFile",
            contentType: false,
            processData: false,
            data: data,
            success: function (message) {
                $("#fileBasket").html(message);
            },
            error: function () {
                $("#fileBasket").html("There was error uploading files!");
            },
            beforeSend: function () {
                $("#progress").show();
            },
            complete: function () {
                $("#progress").hide();
            }
        });
    });

</script>
4
  • 1
    Note $("#fileBasket").on("dragenter", function (evt) { evt.preventDefault(); evt.stopPropagation(); }); $("#fileBasket").on("dragover", function (evt) { evt.preventDefault(); evt.stopPropagation(); }); $("#fileBasket").on("drop", function (evt) { evt.preventDefault(); evt.stopPropagation(); }); could be $("#fileBasket").on("drop dragenter dragover", function (evt) { evt.preventDefault(); evt.stopPropagation(); }); Commented Feb 11, 2018 at 7:41
  • 1
    Try the use of a regular expression to trim everything before final dot var extension = filename.replace(/^.*\./, ''); Commented Feb 11, 2018 at 8:01
  • 1
    stackoverflow.com/a/14852740/125981 Commented Feb 11, 2018 at 8:04
  • Really cool, it worked. Cheers Commented Feb 11, 2018 at 9:55

1 Answer 1

1

Note

$("#fileBasket").on("dragenter", function(evt) {
  evt.preventDefault();
  evt.stopPropagation();
});
$("#fileBasket").on("dragover", function(evt) {
  evt.preventDefault();
  evt.stopPropagation();
});
$("#fileBasket").on("drop", function(evt) {
  evt.preventDefault();
  evt.stopPropagation();
});

could be

$("#fileBasket").on("drop dragenter dragover", function(evt) {
  evt.preventDefault();
  evt.stopPropagation();
});

Try the use of a regular expression to trim everything before final dot

var extension = filename.replace(/^.*\./, '');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.