Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to save an image to SQL server database using MVC application. With current setting the user can select the image file using imageUpload control and it will be shown in image1 control. For experimental purpose source to image2 control is hardcoded by giving the path to the image file. Then when the user clicks the save button the text data will be saved in the database. However, the serialization of the three controls: imageUpload, image1, and image2 controls returns null to the controller. Due to this I am not able to save the image data. Would you please provide me your suggestion to fix this problem?

Here is the code in the view file
@using (Html.BeginForm("", "EditProfile", FormMethod.Post, new { name= "EditingForm", id = "EditingForm", enctype = "multipart/form-data" }))
{

 <label name="msg" id="msg" style="color:blue"></label>
 @Html.TextBox("Name")
 <input id="imageUpload" name="imageUpload"  type="file"  onchange="DisplayImage(this)" /> //display image will show the selected image on image1 control
  <img id="image1" name="image1"  runat="server"  height="190" width="172" />
  <img id="image2" name="image2"  runat="server" src="~/images/laila.png" height="190" width="172" />
  
  <input type="button" class="btn btn-colored" value="Save Profile" onclick="SaveProfileData('e'); " />
}


Here is the code in the Javascript
function SaveProfileData(e) {
    var data = $("#EditingForm").serialize();    
    $.post("/Api/Member/SaveMembersData", data, function (result) {
        msg.innerText = result;
        $("#loaderDiv").hide();
    });    
    return false;
};


And here is the code in the cotroller
[HttpPost]
[Route("Api/Member/SaveMembersData")]
public string SaveMembersData(EditMemberRequestModel m)
{

	if (m.Name == null) //this has the proper value
	{
		return "Name can not be empty";
	}
	else
	{
	//....
	}

	if(m.imageUpload != null) //this return null
	{
		//.....
	}

	if(m.image1 != null) //this return null
	{
	//.....
	}

	if (m.image2 != null) //this return null
	{
	//.....
	}
	
	/*
	.....
	*/
	
}


What I have tried:

I have tried to google some related posts but that didn't help me.
Posted
Comments
F-ES Sitecore 21-May-18 4:49am    
You can't serialise a file upload control like that. google "jquery serialise input file" for alternative methods.
getrelax 21-May-18 5:33am    
Do you have any suggestion for that please?

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