0

I have written this AJAXUpload method in a cshtml file in my C# MVC project:

function AjaxUpload(url, method, data, successFunction, errorFunction, skipErrorDlg) {
    $.ajax({
        contentType: false,
        processData: false,
        url: url,
        data: data,
        type: method,
        dataType: 'json',
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', GlobalAuthToken);
        },
        success: function (data) {
            successFunction(data);
        },
        error: function (event, jqxhr, settings, thrownError) {
            console.log(thrownError);
        }
    });
}

Here is my HTML file component:

<input type="file" class="file" id="attachment" style="display: none;" onchange="fileSelected(this)" accept=".csv"/>

This is my Javascript from where I am calling this above method:

function fileSelected(input) {
    console.log("Chosen file: " + input.files[0].name);

    var data1 = new FormData();
    data1.append("ImportedFile", input.files[0]); 


    AjaxUpload('/Zetes.ZWS.Processes.Rest/api/importbundles',
        'POST',
        data1,
        function () {
            console.log("Import completed");
        }); 
}

However, I am always getting undefined error while invoking this AJAX. So, I just tried hit-and-trial method and found out that when I use processData: false it causes undefined exception.

So, I tried to remove this line too. That gives me Illegal Invocation Exception.

Any suggestions?

jefftrotman
  • 1,059
  • 7
  • 16
Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68

1 Answers1

0

You will have rearrange attribute this postion as like my code. Its working in my application.Hope it will help you.

 $.ajax({
         url: url,
         type: "POST",
         data: data,
         contentType: false,
         processData: false,
         success: function (data) {
           successFunction(data);
         },
         error: function (event, jqxhr, settings, thrownError) {
           console.log(thrownError);
         }
 });

also you can try this

Naveen
  • 1,441
  • 2
  • 16
  • 41
  • I tried exactly your solution. It is still error for me. May some setting is missing which is not allowing me to use processData: false? – Srijani Ghosh Aug 13 '19 at 12:09
  • Thanks for your help. This is not working too. I am starting to believe that there must be some other problem with the framework I am working on which might be preventing me sending files to server or something like that – Srijani Ghosh Aug 13 '19 at 12:43