Click here to Skip to main content
15,914,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo,
I designed the kendo form using kendoupload control i can upload my image file.Here i specified one upload button.When i click that upload button the image file has to be saved to the database and display it in the same page.for example i give the id to the type file in the form which is fileupload.When i use fileupload:$('#fileupload'). When i use that syntax i transfers only the path to the controller and in controller it saves it to the database.So my problem is i had to send image to the database not the path.So in controller by using HttpPostedFileBase am calling the files.So please help me how can we transfer image file from view to controller and from controller to the database.So please help me in finding out the solution.
Posted

1 solution

Hope you may get an idea by the below code.
This is the way what we used.

[HttpPost]
        public ActionResult uploadOrganisationImage(FormCollection collection)
        {
            System.Guid guid = System.Guid.NewGuid();
            try
            {

                string OrganisationImg = collection.Get("OrganisationImg");               
                string Filename = "";
                string FileExtension = "";
                string[] parts;
                byte[] img;
                if (OrganisationImg != null)
                {

                    parts = this.HttpContext.Request.Files[0].FileName.Split(new char[] { '\\' });
                    Filename = parts[parts.Length - 1];
                    FileExtension = Filename.Split(new char[] { '.' })[1];
                    img = new byte[Request.Files[0].ContentLength];

                    using (BinaryWriter writer = new BinaryWriter(new FileStream(Server.MapPath("../Content/" + OrganisationID + "/images") + "\\" + guid.ToString() + "." + FileExtension, FileMode.Create)))
                    {
                        Request.Files[0].InputStream.Read(img, 0, Request.Files[0].ContentLength);
                        writer.Write(img);
                    }
                    
                        orgConfig.saveOrganisationImage(long.Parse(OrganisationID), guid.ToString() + "." + FileExtension); (business logic goes here)
                }

                string xml = orgConfig.getOrgCSSConfig(long.Parse(OrganisationID));

                return this.Content(utilS.xslTransform(Server.MapPath("../Views/OrganisationConfig/xsl/ImageGrid.xslt"), xml.ToString()
                    , new string[][] { 
                        new string[] { "OrganisationID", OrganisationID.ToString() },
                        new string[] { "Time", this.Time.ToString()},
                        new string[] {"ApplicationRoleID",this.RoleID.ToString()}
                    }));
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return View();
            }
        }
 
Share this answer
 
v2

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