Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys how can i upload an image to my folder using AJAX in MVC

This is my View:
<pre lang="ASP.NET"><pre>   <div class="pb-2 shadow" style="background-color: gray;  height: 150px; width: 150px ; position:relative; align-self:center" id="employee-pic-preview">
                            <img src="#" alt="Employee Pic" style="height:inherit;width:inherit" id="employeepic" />

And this is my ModulesController:
<pre>  [HttpPost]
        public ActionResult UploadFiles(HttpPostedFileBase employeepic)
            if (employeepic != null)
                string path = @"\\\AQSImages\IdPictures\";
                if (!Directory.Exists(path))
                employeepic.SaveAs(path + Path.GetFileName(employeepic.FileName));
            return View();

and this is my AJAX code:
EmployeeManager.UploadFiles = function () {
    var fileUpload = $("#employeepic");
    var files = fileUpload.files;

    // Create  a FormData object
    var fileData = new FormData();
    fileData.append(files[i].name, files[i]);
        url: '/Modules/UploadFiles', //URL to upload files 
        type: "POST", //as we will be posting files and other method POST is used
        processData: false, //remember to set processData and ContentType to false, otherwise you may get an error
        contentType: false,
        data: fileData,
        success: function (result) {
        error: function (err) {

What I have tried:
Updated 14-Jun-22 4:56am
Graeme_Grant 13-Jun-22 18:47pm    
We can not see from your question above what is happening, nor can we see your screen from here.

You need to update your question with what happens - Was there an error? Is the Controller being called from the Ajax code? Does the Ajax code send anything?

A quick look at the SO answer and your code, you are not explicitly following what is said. I would be setting a breakpoint there and see exactly what is happening.

1 solution

fileData.append(files[i].name, files[i]);
You are using an undeclared variable i. Unless you've declared that somewhere else, it will be undefined, so you will get a reference error. And if you have declared it somewhere else, it almost certainly doesn't contain the value you expect it to contain.

Also, fileUpload is a jQuery object, which doesn't contain a files property. So that won't work either.

And based on your markup, employeepic is an <img>, not an <input type="file">.

Start by fixing your code:
<input type="file" id="employeepic" name="employeepic">
const fileUpload = document.getElementById("employeepic");
const files = fileUpload.files;

// Create  a FormData object
const fileData = new FormData();
fileData.append(, files[0]);
If it still doesn't work, then check your browser's developer console for errors. If there are none, check the network requests tab to examine precisely what you are sending to the server.
Share this answer
Ramon Ortega Jr 14-Jun-22 16:24pm    
Thank you very much sir this it works !!!!! Thank you!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900