Skip to content Skip to sidebar Skip to footer

How To Send Javascript File Object+ Data To Mvc 6 Controller - Asp.net 5

This Ajax is inside a function that I know has the file and properties in. Nothing is happening, I am dragging my file and dropping it but nothing is happening with the Ajax. When

Solution 1:

To get the file in the controller, you have to send the full file.

Try something like:

AJAX:

 $.ajax({
            type: "POST",
            url: "HomeController/Index", // the method we are callingcontentType: "application/json; charset=utf-8",
            data: { file: file, fileType: file.type, fileSize: file.size },
            dataType: "json",
            success: function(result) {
                alert('Yay! It worked!');
                // Or if you are returning somethingalert('I returned... ' + result.WhateverIsReturning);
            },
            error: function(result) {
                alert('Oh no :(');
            }
        });

Controller:

public IActionResult Index(HttpPostedFileBase file, string fileType, int fileSize)
    {
        ViewBag.Environments = _configHelper.GetEnvironments();
        var model = new HomeViewModel { Environment = "DEV" };
        return View(model);
    }

Post a Comment for "How To Send Javascript File Object+ Data To Mvc 6 Controller - Asp.net 5"