upload and save image as binary data using asp.net mvc5

/* ############### METHOD 1: ############### WORKABLE CODE BY SAVING UPLOADED FILE IN FILESYSTEM FIRST, THEN CONVERT INTO BYTE ARRAY AND SAVE IN DATABASE
public ActionResult Create([Bind]ViewModel argViewModel, HttpPostedFileBase file)
{
        using (EntitiesModel1 context = new EntitiesModel1())
        {
                        if (file != null && file.ContentLength > 0)
                        {
                            // extract only the fielname
                            var fileName = System.IO.Path.GetFileName(file.FileName);
                            // store the file inside ~/App_Data/uploads folder
                            var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                            file.SaveAs(path);
                        }

                        // Update fields
                        DOCUMENTS_DATA obj = new DOCUMENTS_DATA();
                        obj.DOCUMENT= new byte[file.ContentLength];
                        file.InputStream.Read(obj.DOCUMENT, 0, file.ContentLength);
                        obj.DOCUMENT_ID = 10006;
                        obj.DOCUMENT_TYPE_ID = 10001;
                        context.Add(obj);
                        context.SaveChanges();
          }
}

/* ############### METHOD 2: ###############
                         * WORKABLE CODE BY MAINTAINING UPLOADED FILE IN INPUTSTREAM FIRST,
                         * THEN CONVERT INTO BYTE ARRAY AND SAVE IN DATABASE
                         * #########################################*/
                        if (file != null && file.ContentLength > 0)
                        {
                            byte[] imageByte = null;
                            using (var binaryReader = new System.IO.BinaryReader(file.InputStream))
                            {
                                imageByte = binaryReader.ReadBytes(file.ContentLength);

                                //CHECKING WHETHER IF DOCUMENTS OBJECT ALREADY EXISTS OR NOT, IF YES THEN UPDATE RESPECTIVE DOCUMENT TYPE ELSE CREATE NEW OBJECT AND INSERT DOCUMENT
                                if (entityObj != null && entityObj.DOCUMENTS_DATAs != null && entityObj.DOCUMENTS_DATAs.Where(w => w.DOCUMENT_TYPE_ID == 10001).Count() >0)
                                {
                                    entityObj.DOCUMENTS_DATAs.FirstOrDefault(w => w.DOCUMENT_TYPE_ID == 10001).DOCUMENT = imageByte;
                                }
                                else
                                {
                                    entityObj.DOCUMENTS_DATAs.Add(new DOCUMENTS_DATA() { DOCUMENT_TYPE_ID = 10001, DOCUMENT = imageByte });
                                }
                            }
                        }

Comments

Popular posts from this blog

storing byte array as column value in datatable using c#

post form data and bind with model : using jquery ajax in mvc

Htc sensation XL mobile not working with windows 7 or above, along with Htc sync software