0

I have found multiple examples of file uploading but most of them use forms or it is by itself and the only thing being passed is the file.

I have a page already passes data via Ajax to a controller. I would like to add a file upload option to that existing Ajax call. The file will be stored in a sql database so I'm assuming that I will need to pass the data or convert it to a byte array.

My ajax call looks like this:

 $("#btnNewTicket").click(function () {                  //Grabbing the Click Event of btnNewTicket
        var newTickTitle = $("#txtNewTickTitle").val();
        var newTickDesc = $("#txtNewTickDesc").val();
        var newTickCat = $("#ddCatList").val();

        $.ajax({
            url: '@Url.Action("AddTicket","HelpDesk")',
            data: { 'TickTitle': newTickTitle, 'TickDesc': newTickDesc, 'TickCat': newTickCat },
            type: 'GET',
            success: function (data) {
                $("#loadpartial").empty();
                alert("Ticket Successfully Submitted");
                window.location.reload();
            },

            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
                alert(xhr.status);
            }
        });

Is there a fairly straight forward way to do this simply? Like basically adding another variable to the Ajax call? Uploading a file seems so simple. Or I could just not realize how involved it really is.

tereško
  • 58,060
  • 25
  • 98
  • 150
GarudaLead
  • 469
  • 1
  • 5
  • 18
  • Out of curiosity, why do you have window.location.reload() when you're using ajax? – artm Sep 11 '14 at 13:47
  • GET should only be used to retrieve informatino, use put or post – Magic-Mouse Sep 11 '14 at 13:49
  • You can upload files with jQuery using the $.ajax() method if `FormData` and the `File API` are supported (both HTML5 features). Reference link :- http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – Pranav Sep 11 '14 at 13:50

1 Answers1

0

There are plugins available to upload a file via ajax. Recently i used this plugin to upload file via ajax in asp.net mvc application.

ASP.NET MVC: Simple example of ajax file upload using jQuery

I also made a tutorial on ajax file uploading which is without plugin but it uses jquery and iframe approach to upload file via ajax.

Asp.net mvc Ajax File Uploading using JQuery

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160