Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // POST: /LedgerUser/Create[Bind(Prefix = "LedgerUser")]
- [HttpPost]
- public ActionResult Create(bool Thumbnail,LedgerUser LedgerUser, HttpPostedFileBase imageLoad2)
- {
- var Avatar = new Accounts.Models.Image();
- //--Handle the image first.
- if (imageLoad2 != null)
- {
- using (System.Drawing.Image img = System.Drawing.Image.FromStream(imageLoad2.InputStream))
- {
- //--Initalise the size of the array
- byte[] file = new byte[imageLoad2.InputStream.Length];
- //--Create a new BinaryReader and set the InputStream for the Images InputStream to the
- //--beginning, as we create the img using a stream.
- BinaryReader reader = new BinaryReader(imageLoad2.InputStream);
- imageLoad2.InputStream.Seek(0, SeekOrigin.Begin);
- //--Load the image binary.
- file = reader.ReadBytes((int)imageLoad2.InputStream.Length);
- //--Create a new image to be added to the database
- Avatar.id = Guid.NewGuid();
- Avatar.TableLink = LedgerUser.id;
- Avatar.RecordStatus = " ";
- Avatar.FileSize = imageLoad2.ContentLength;
- Avatar.FileName = imageLoad2.FileName;
- Avatar.FileSource = file;
- Avatar.FileContentType = imageLoad2.ContentType;
- Avatar.FileHeight = img.Height;
- Avatar.FileWidth = img.Width;
- //-- Now we create the thumbnail and save it.
- if (Thumbnail == true)
- {
- byte[] thumbnail = Images.CreateThumbnailToByte(imageLoad2.InputStream, 100, 100);
- Avatar.ThumbnailSource = thumbnail;
- Avatar.ThumbnailFileSize = thumbnail.Length;
- Avatar.ThumbnailContentType = Files.GetContentType(imageLoad2.FileName);
- Avatar.ThumbnailHeight = Images.FromByteHeight(thumbnail);
- Avatar.ThumbnailWidth = Images.FromByteWidth(thumbnail);
- }
- else
- {
- byte[] thumbnail = new byte[0];
- Avatar.ThumbnailSource = thumbnail;
- Avatar.ThumbnailFileSize = 0;
- Avatar.ThumbnailContentType = " ";
- Avatar.ThumbnailHeight = 0;
- Avatar.ThumbnailWidth = 0;
- }
- }
- }
- if (!ModelState.IsValid)
- {
- ModelState.ModelStateErrors();
- }
- if (ModelState.IsValid)
- {
- using (AccountsEntities context = new AccountsEntities())
- {
- try
- {
- //--Save the LedgerUser
- context.LedgerUsers.AddObject(LedgerUser);
- context.SaveChanges();
- //--Save the Image
- context.Images.AddObject(Avatar);
- context.SaveChanges();
- return RedirectToAction("Index", "Home");
- }
- catch (OptimisticConcurrencyException)
- {
- context.Refresh(RefreshMode.ClientWins, LedgerUser);
- context.SaveChanges();
- Console.WriteLine("OptimisticConcurrencyException " + "handled and changes saved");
- }
- catch (UpdateException ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- var userTypes = new SelectList(db.UserTypes, "id", "Description");
- var ledgerUser = new LedgerUser()
- {
- id = LedgerUser.id,
- RecordStatus = LedgerUser.RecordStatus,
- CreatedDate = LedgerUser.CreatedDate,
- DateOfBirth = LedgerUser.DateOfBirth
- };
- var viewModel = new LedgerUserViewModel()
- {
- UserTypes = userTypes,
- LedgerUser = ledgerUser
- };
- return View(viewModel);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement