0

I want to upload multiple files from multiple button clicks in asp.net core razor view.

Below is my jquery code which is not working.

<script type="text/javascript" src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $('#btnupload1').on('click', myFunction('#fileupload1'));
        $('#btnupload2').on('click', myFunction('#fileupload2'));
        $('#btnupload3').on('click', myFunction('#fileupload3'));
    })

    function myFunction(value) {
        var fdata = new FormData();
        var fileUpload = $(value).get(0);
        var files = fileUpload.files;
        fdata.append(files[0].name, files[0]);
        $.ajax({
            type: "POST",
            url: "/Contract/UpFiles",
            beforeSend: function(xhr) {
                xhr.setRequestHeader("XSRF-TOKEN",
                    $('input:hidden[name="__RequestVerificationToken"]').val());
            },
            data: fdata,
            contentType: false,
            processData: false,
            success: function(response) {
                if (response.length == 0)
                    alert('Some error occured while uploading');
                else {
                    $('#divPrint').html(response);
                }
            },
            error: function(e) {
                $('#divPrint').html(e.responseText);
            }
        });
    }
</script>
6
  • Could you provide what kind of error message you get? Commented May 13, 2021 at 19:41
  • this is not working as expected, not getting any error Commented May 13, 2021 at 20:36
  • If you are not getting any error it is probably because your buttons were not bind to any function as you expected. In those situations you may check one of those buttons if there is event by right click ( Inspect ) or from Developer tools / Inspector. Commented May 13, 2021 at 21:04
  • if i display alert("msg") then it is showing after just load the form. if i remove the alert funciton then it is in silent. Commented May 13, 2021 at 21:07
  • My requirement is , to call same upload function from different different file upload button. Commented May 13, 2021 at 21:10

2 Answers 2

1

One possible problem is when you bind your events to those buttons probably not there. Solution 1 :

$(document).ready(function () {
 // your code 

});

Another solution if you are creating your buttons dynamically with other JS

    $(document.body).on('click', '#btnupload1', function (e) {
 //  
myFunction('#fileupload1')

    })
    $(document.body).on('click', '#btnupload2', function (e) {
 //  
myFunction('#fileupload2')

    })

    $(document.body).on('click', '#btnupload3', function (e) {
 //  
myFunction('#fileupload3')

    })
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry I was missing the correct way to call. I have resolved my problem to change below code.

Thanks

 $('#btnupload').on('click', function () { myFunction('#fileupload'); });
    $('#btnupload2').on('click', function () { myFunction('#fileupload2'); });
    $('#btnupload3').on('click', function () { myFunction('#fileupload3'); });
    $('#btnupload4').on('click', function () { myFunction('#fileupload4'); });
    $('#btnupload5').on('click', function () { myFunction('#fileupload5'); });

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.