Jquery File Upload : Database call after successful images upload

I’m using jQuery file upload for file uploads with MVC and Web API. Using the default behaviour singleFileUploads = true (By default each file of a selection is uploaded using an individual /request for XHR type uploads).

I am trying to save total number of images and some other data in final database insert call once all the images uploaded to server is completed. The other problem is web server doesnt have access to file server, only app server can save and read images from file server.

I know similar question already there but both have different environments and issues. running SQL after successful upload with jQuery-File-Upload

Here is code : I tried using all callback functions done, completed and finished but every function gets call after each file upload. i am trying to call SaveDataDB() only once when all file uploads completed.

$(function () {

    $form = $('#fileupload').fileupload({
        dataType: 'json',
        singleFileUploads: true,
        sequentialUploads:false,
        url: "/FileUpload/Upload",
        add: function (e, data) {
            $("#btnStart").on("click", function () {
                data.submit();
            })
        },
        completed: function (e, data) {
            $.ajax({
                url: "/FileUpload/SaveDataDB",
                type: "POST",
                dataType: "json"
            });
        },
    }).bind('fileuploadcompleted', function (e, data) { alert("Message", "Title"); })
      .bind('fileuploadfinished', function (e, data) { alert("fileuploadfinished", "Title") })
      **.bind('fileuploadstop', function (e, data) {
            $.ajax({
                url: "/FileUpload/SaveDataDB",
                type: "POST",
                dataType: "json",
                data: TotalFiles,
                success: function (data) {
                };**

});


MVC Controller

 [HttpPost]
    public JsonResult Upload()
    {
        var resultList = new List<ViewDataUploadFilesResult>();

        var CurrentContext = HttpContext;

        filesHelper.UploadAndShowResults(CurrentContext, resultList);
        JsonFiles files = new JsonFiles(resultList);
        bool isEmpty = !resultList.Any();
        if (isEmpty)
        {
            return Json("Error ");
        }
        else
        {
            return Json(files);
        }
    }

    [HttpPost]
    public JsonResult SaveDataDB()
    {
        return Json(new { status = "Success", message = "Success" });
    }

Leave a Reply

Your email address will not be published. Required fields are marked *