Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to drag and drop a .pdf file and then pass its name as a parameter to controller. I don't want to save the file in any directory.
I have the drop area and the list group to show file name:
C#
<h2>Drag & Drop your file</h2>
<div id="dropArea">
    Drop your file here
</div>
<h4>Dropped files : </h4>
<ul class="list-group" id="uploadList"></ul>


What I have tried:

C#
<script type="text/javascript">
        $(document).ready(function () {
            alert("Hi");
            $("#dropArea").on("dragover", function (event) {
                event.preventDefault();
                event.stopPropagation();
                $(this).addClass('active-drop');
            });
            $("#dropArea").on("dragleave", function (event) {
                event.preventDefault();
                event.stopPropagation();
                $(this).removeClass('active-drop');
            });
            $("#dropArea").on("drop", function (event) {
                event.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "Home/Index",
                    contentType: "application/json; charset=utf-8",

                    data: { // How do I pass the file's name to pass it here?? },

                    dataType: "json",
                    success: function (result) {
                        alert('Worked!');
                    },
                    error: function (result) {
                        alert("Didn't work");
                    }
                });
   $('#uploadList').append('<li class="list-group-item">' + /*fileName*/ + '</li>')
                alert("Dropped!");
            });
        });
</script>

FIRST: dragover and dragleave are not triggered.
SECOND: How to pass file name in data section of ajax?
Posted
Updated 27-Mar-18 5:35am
v2
Comments
F-ES Sitecore 27-Mar-18 11:31am    
Passing the filename isn't going to help you. Google "jquery drag drop upload file" and I'm sure you'll find lots of examples of what you need to do to support this.
TempoClick 27-Mar-18 11:36am    
@F-ES Sitecode, I have read many articles but they're all about uploading the file and saving it. What I only need here is pass the name of the dropped file.

1 solution

There is a working of example of how to do this here;

Drag and drop file upload with jQuery and AJAX[^]
 
Share this answer
 
Comments
TempoClick 27-Mar-18 11:39am    
@F-ES, 404: page not found!
F-ES Sitecore 27-Mar-18 12:17pm    
Mmm, works ok for me. It's on the first page of results from the google I suggested though, it's not hard to find examples of these things.
Richard Deeming 27-Mar-18 12:28pm    
The link works fine for me. Are you behind or corporate (or government) firewall?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900