Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using MVC3 to upload a file using ajax,i have made a controller to upload file

 public JsonResult ChangeImage(HttpPostedFileBase file)
        {
            UserProfileModel model=new UserProfileModel();
            string path = "";
            string imgpath = "";
            string upath = ConfigurationManager.AppSettings["imagepath"];
            string thumbnail = "";
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                //fileName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
                if (IsImage(file.FileName.ToString()))
                {

                    fileName = "Image_" + Session["uid"].ToString() + "_" + fileName;
                    path = Path.Combine(Server.MapPath(upath), fileName);
                    file.SaveAs(path);
                    imgpath = fileName;
                    ViewData["img"] = imgpath;                 
                        }
                    }

                }
}


and on my cshtml file i'm using this code
C#
function UpdateAvailableDate(img) {
       $.post("/UserProfile/ChangeImage", { "img": img },
                           function (data) {
                               if (data == 'true') {
                                   alert('Success');

                               }
                               else
                                   alert('Failed');
                           });

   }

   $('#updnewimg').live('click', function () {
       alert('hi');
       var img = $('#newimg').val();
       if (img == null || img == "") {
           alert('Select an image');
       }
       else {
           // itemId = $(this).closest('#posted-book').find('#HiddenId')[0].value;
           UpdateAvailableDate(img);
       }
   });


newimg is the id of my upload control
HTML
<input type="file" id="newimg">


and updnewimg is my button to submit form
HTML
<input type="button" id="updnewimg">


The problem is that, when it comes to controller(ChangeImage) the value of file appears as null ie the parameter of the controller is appearing null

Can anyone gives solution for this problem?
Regards
Posted
Updated 25-Dec-11 20:40pm
v2

1 solution

Few days ago i got same problem. I resolved it like follwoing

C#
<input type="file" id="newimg">


use the same parameter name in controller. Means "newimg" use instead of "file". Then try it. Then also if it wont work follow the following js for upload

SQL
document.frmname.action = ../controllername/methodname;
document.frmname.form = frmname;
document.frmname.method = "post"
document.frmname.submit();
 
Share this answer
 

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