Beispiel #1
0
        public MongoPictureModel TransformToImage(HttpPostedFileBase theFile)
        {
            if (theFile.ContentLength > 0)
            {
                //get the file's name
                string theFileName = Path.GetFileName(theFile.FileName);

                //get the bytes from the content stream of the file
                byte[] thePictureAsBytes = new byte[theFile.ContentLength];
                using (BinaryReader theReader = new BinaryReader(theFile.InputStream))
                {
                    thePictureAsBytes = theReader.ReadBytes(theFile.ContentLength);
                }

                //convert the bytes of image data to a string using the Base64 encoding
                string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);

                //create a new mongo picture model object to insert into the db
                MongoPictureModel thePicture = new MongoPictureModel()
                {
                    FileName            = theFileName,
                    PictureDataAsString = thePictureDataAsString
                };

                return(thePicture);
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// This method will insert the picture into the db
        /// </summary>
        /// <param name="thePicture"></param>
        /// <returns></returns>
        private async Task <bool> InsertPictureIntoDatabase(MongoPictureModel thePicture)
        {
            var thePictureColleciton = pictureCollection;

            try
            {
                await thePictureColleciton.InsertOneAsync(thePicture);

                return(true);
            }
            catch { return(false); }
        }
Beispiel #3
0
        public async Task <ActionResult> AddPicture(HttpPostedFileBase theFile)
        {
            if (theFile.ContentLength > 0)
            {
                //get the file's name
                string theFileName = Path.GetFileName(theFile.FileName);

                //get the bytes from the content stream of the file
                byte[] thePictureAsBytes = new byte[theFile.ContentLength];
                using (BinaryReader theReader = new BinaryReader(theFile.InputStream))
                {
                    thePictureAsBytes = theReader.ReadBytes(theFile.ContentLength);
                }

                //convert the bytes of image data to a string using the Base64 encoding
                string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);

                //create a new mongo picture model object to insert into the db
                MongoPictureModel thePicture = new MongoPictureModel()
                {
                    FileName            = theFileName,
                    PictureDataAsString = thePictureDataAsString
                };

                //insert the picture object
                bool didItInsert = await InsertPictureIntoDatabase(thePicture);

                if (didItInsert)
                {
                    ViewBag.Message = "The image was updated successfully";
                }
                else
                {
                    ViewBag.Message = "A database error has occurred";
                }
            }
            else
            {
                ViewBag.Message = "You must upload an image";
            }

            return(View());
        }
Beispiel #4
0
        public async Task <PedidosModel> GerarPedido(Endereco endereco)
        {
            var user = App_Start.SessionContext.Instance.GetUserData();
            List <PedidoElementModel> cartItems = (List <PedidoElementModel>)PIDI.App_Start.SessionManager.ReturnSessionObject("items");

            var generatedOrder = new PedidosModel();

            generatedOrder.userId    = user.userId;
            generatedOrder.OrderDate = DateTime.Now;
            //generatedOrder.OrderDate = DateTime.Parse("16/10/2019 20:31:19"); usei para teste de filtragem
            generatedOrder.paymentType          = "paypal";
            generatedOrder.State                = endereco.uf;
            generatedOrder.City                 = endereco.cidade;
            generatedOrder.Country              = "Brasil";
            generatedOrder.Address              = endereco.rua;
            generatedOrder.orderState           = "Aguardando Pagamento";
            generatedOrder.PostalCode           = endereco.cep;
            generatedOrder.produtosRequisitados = cartItems;

            for (int i = 0; i < orderImages.Count; i++)
            {
                var target = generatedOrder.produtosRequisitados[i];
                var key    = target.produtoRequisitado.Id.ToString();
                if (orderImages.ContainsKey(key))
                {
                    var img = new MongoPictureModel();
                    img.PictureDataAsString = orderImages[key];
                    img.id = ObjectId.GenerateNewId();

                    target.userImage = img;
                }
            }


            generatedOrder.Total = GerarTotal(cartItems);
            PedidosModel x = await CriarPedido(generatedOrder);

            return(x);
        }