Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
  1. Home
  2. Blogpost

How to upload Image file using AJAX and jquery

Date- Feb 26,2021

14229

Ajax call

Ajax 

So starting of from the beginning Ajax is used for Asynchronous Javascript and XML. We can use it for many purposes. Few basic uses of Ajax are:-

  • Update page without reloading the page providing better performance.
  • Request data from a server - after the page has loaded which can be used in loading Partial Views.
  • Send data to a server without reload - in the background making it easier to performance Save, Delete operations smoothly.

Ajax in Asp.Net MVC

Ajax can be used anywhere where we can use jquery. So you have seen various ways of using Ajax in our previous article  Using Ajax in Asp.Net MVC. But posting fileupload files becomes a little complicated with Ajax.So we will learn How to upload Image file using AJAX and jquery. So let's start from beginning:-

Step-1

So you have to add a fileupload filed on your view and now we will make some changes in Ajax to post that file on controller. We have also kept the three textboxes for some dummy data.

So for posting files and other data we have to send data by adding to formdata.

FormData is introduced to allow developers to build forms objects dynamically (and can even include files from the user's file system), and then to send this form object via AJAX.

So we will create a new formdata object and than add all the required values in that formdata using formdata.append. You will also add the file here . Remember to use the same keys which you have used on controller to receive tha data.

One thing to remember here is you have to use contenttype false which was json in previous methods we have seen in last article.And also set processdata to false

    $(function () {
        $("#btnGet6").click(function () {
            var formData = new FormData();
            var empIds = $("#txtId").val();
            var empNames = $("#txtName").val();
            var empSalarys = $("#txtSalary").val();
            var totalFiles = document.getElementById("FileUpload").files.length;
            debugger;
            for (var i = 0; i < totalFiles; i++) {
                var file = document.getElementById("FileUpload").files[i];
                formData.append("fileUpload", file);
                formData.append("empId", empIds);
                formData.append("empName", empNames);
                formData.append("empSalary", empSalarys);
            }
            $.ajax({
                type: 'post',
                url: '/Home/AjaxMethodFileUpload',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (response) {
                    debugger;
                    alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + " Your Salary Is: " + response.EmpSalary + " And Your File Name Is: " + response.File);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
        });
    });

On the controller side we have to using same way like we have used earlier for first three parameters. Like giving same name which we have added in formdata. And then we will use a parameter of type of HttpPostedFileBase to receive the file.You will get you file in this parameter. Also you can get the file in Request.Files. So this is how you can get your file posted in mvc.

 
        [HttpPost]
        public JsonResult AjaxMethodFileUpload(string empId, string empName, string empSalary, HttpPostedFileBase fileUpload)
        {
            PersonModel personModel = new PersonModel
            {
                EmpId = empId,
                EmpName = empName,
                EmpSalary = empSalary,
                File = fileUpload.FileName
            };
            return Json(personModel);
        }

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Implement Stripe Payment Gateway In ASP.NET
Sep 10, 2020
Jquery Full Calender Integrated With ASP.NET
Sep 30, 2020
Microsoft Outlook Add Appointment and Get Appointment using Asp.Net MVC
Oct 03, 2020
How to implement JWT Token Authentication and Validate JWT Token in ASP.NET MVC using JWT
Oct 12, 2022

Comments

Contents

More in ASP.NET MVC

  • Payumoney Integration With Asp.Net MVC 23090 views
  • MVC Crud Operation with Interfaces and Repository Pattern wi… 21784 views
  • Using Ajax in Asp.Net MVC 21130 views
  • Stopping Browser Reload On saving file in Visual Studio Asp.… 20550 views
  • Exception Handling and Creating Exception Logs in Asp.net MV… 20386 views
View all ASP.NET MVC posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page