Exemplo n.º 1
0
        public async Task <bool> UploadAsync(IFileListEntry fileEntry)
        {
            if (fileEntry != null)
            {
                var format       = "image/jpeg";
                var imageFileMin = await fileEntry.ToImageFileAsync(format, 640, 480);

                var imageFileMax = await fileEntry.ToImageFileAsync(format, 1024, 768);

                var pathMin = Path.Combine(_appEnvironment.WebRootPath, "upload/images_min", fileEntry.Name);
                var pathMax = Path.Combine(_appEnvironment.WebRootPath, "upload/images_max", fileEntry.Name);

                var ms = new MemoryStream();
                await imageFileMin.Data.CopyToAsync(ms);

                using (FileStream fileStream = new FileStream(pathMin, FileMode.Create, FileAccess.Write))
                {
                    ms.WriteTo(fileStream);
                }
                ms.Dispose();
                ms = new MemoryStream();
                await imageFileMax.Data.CopyToAsync(ms);

                using (FileStream fileStream = new FileStream(pathMax, FileMode.Create, FileAccess.Write))
                {
                    ms.WriteTo(fileStream);
                }

                FileModel file = new FileModel {
                    Name = fileEntry.Name, Path = "upload/images"
                };
                _context.Files.Add(file);
                _context.SaveChanges();

                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// this will be the main function linked to the Drag N Drop on the
        /// </summary>
        /// <param name="files">the selected files</param>
        /// <returns>the current Task Thread</returns>
        async Task SelectLogo(IFileListEntry[] files)
        {
            //we get the first and most likely only file from the list
            IFileListEntry file = files.FirstOrDefault();

            //if no file was selected we return
            if (file == null)
            {
                return;
            }
            else
            {
                //we set the loading status
                Status = "Se incarca...";

                ImageDimensions = await jsRuntime.InvokeAsync <Size>("getElementSize", "inputFile");

                //then using the fileReader
                //we need the component dimmension for the div to format the image to the correct sizes


                //we retrieve the image info
                //if we have the settings to mantain aspect ratio we need to reformat the image
                //we initialize a new ImageFile
                IFileListEntry ImageFile;
                if (PageController.MaintainAspectRatio)
                {
                    //we get the image file from the reformated image
                    ImageFile = await file.ToImageFileAsync(PageController.ImageFormat, (Int32)Math.Round(ImageDimensions.Width), (Int32)Math.Round(ImageDimensions.Height));
                }
                else
                {
                    //we get the image file from the file directly
                    ImageFile = file;
                }

                //we initialize a new memory stream
                MemoryStream ms = new MemoryStream();
                //get all the data from the Image file into the memeory stream
                await ImageFile.Data.CopyToAsync(ms);

                //then dump the stream into a bite array and set it to the PageControllers LogoBaseProperty
                PageController.LogoBase = ms.ToArray();
                //we also set the status back to the default value
                Status = DefaultStatus;
            }
        }