Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying this way but i got error please Help me Friends....


C#
public ActionResult Upload()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Upload(Image obj, HttpPostedFileBase aFile)
        {

            MySqlCommand cmd11 = new MySqlCommand("Select Count(*) from student", Con);
            Con.Open();
            int i = Convert.ToInt32(cmd11.ExecuteScalar()) + 1;
            GenId = i + DateTime.Now.ToString("ss");


            //HttpPostedFileBase aFile = Request.Files["Filedata"];
            //int contentLength = aFile.ContentLength;
            //byte[] bytePic = new byte[contentLength];
            //aFile.InputStream.Read(bytePic, 0, contentLength);

            string filePath = Path.GetFileName(aFile.FileName);
            string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(aFile.FileName));
            aFile.SaveAs(Server.MapPath("~/Content/Images/" + filePath));

            MySqlCommand cmd = new MySqlCommand("Insert into student values(@Id,@Name,@EmailId,@MobileNo,@City,@Image", Con);
            cmd.Parameters.AddWithValue("@Id", GenId);
            cmd.Parameters.AddWithValue("@Name", obj.Name);
            cmd.Parameters.AddWithValue("@EmailId", obj.EmailId);
            cmd.Parameters.AddWithValue("@City", obj.City);
            cmd.Parameters.AddWithValue("@MobileNo", obj.MobileNo);
            cmd.Parameters.AddWithValue("@Image", savedFileName);
            cmd.ExecuteNonQuery();
            return View();
        }


What I have tried:

public ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(Image obj, HttpPostedFileBase aFile)
{

MySqlCommand cmd11 = new MySqlCommand("Select Count(*) from student", Con);
Con.Open();
int i = Convert.ToInt32(cmd11.ExecuteScalar()) + 1;
GenId = i + DateTime.Now.ToString("ss");


//HttpPostedFileBase aFile = Request.Files["Filedata"];
//int contentLength = aFile.ContentLength;
//byte[] bytePic = new byte[contentLength];
//aFile.InputStream.Read(bytePic, 0, contentLength);

string filePath = Path.GetFileName(aFile.FileName);
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(aFile.FileName));
aFile.SaveAs(Server.MapPath("~/Content/Images/" + filePath));

MySqlCommand cmd = new MySqlCommand("Insert into student values(@Id,@Name,@EmailId,@MobileNo,@City,@Image", Con);
cmd.Parameters.AddWithValue("@Id", GenId);
cmd.Parameters.AddWithValue("@Name", obj.Name);
cmd.Parameters.AddWithValue("@EmailId", obj.EmailId);
cmd.Parameters.AddWithValue("@City", obj.City);
cmd.Parameters.AddWithValue("@MobileNo", obj.MobileNo);
cmd.Parameters.AddWithValue("@Image", savedFileName);
cmd.ExecuteNonQuery();
return View();
}
Posted
Updated 19-Jul-16 22:20pm
Comments
F-ES Sitecore 20-Jul-16 4:15am    
What error and what line?
Member 11652153 20-Jul-16 6:33am    
string filePath = Path.GetFileName(aFile.FileName);

aFile value is Null
F-ES Sitecore 20-Jul-16 7:04am    
There's probably a problem with your view, but you haven't posted that code. See the links posted in solution 1, this is a commonly asked question and examples are well documented.
Member 11652153 20-Jul-16 7:08am    
@using (Html.BeginForm("Upload","Image",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="file" id="ImagePath" /><br><br>


<br><br>
@ViewBag.Message
}
F-ES Sitecore 20-Jul-16 7:18am    
Your input is named "file" but the action parameter is "aFile". The two need to match so change your action

public ActionResult Upload(Image obj, HttpPostedFileBase file)

this is basic MVC 101, I'd advise you to go through a book or tutorials on MVC to learn the basics before trying something complicated like this.

1 solution

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