Ejemplo n.º 1
0
        public async Task <IActionResult> Upload(UploadViewModel model, IList <IFormFile> files)
        {
            if ((model != null) && ModelState.IsValid)
            {
                var user = await GetLoggedInUser();

                var containerName = user.CompanyId.ToString().ToLower();
                await _blobService.CreateContainerIfNotExists(containerName);

                foreach (var formFile in files)
                {
                    if ((formFile != null) && (formFile.Length > 0))
                    {
                        using (var fileStream = formFile.OpenReadStream())
                        {
                            await
                            _blobService.UploadBlobFromStream(containerName, formFile.FileName, fileStream,
                                                              formFile.ContentType, false);
                        }
                        var file = new FileInfo
                        {
                            ContentType     = model.ContentType,
                            FileName        = formFile.FileName,
                            FileContentType = formFile.ContentType,
                            FileSizeInBytes = formFile.Length,
                            ContainerName   = containerName,
                            Description     = model.Description,
                            ReadOnly        = model.ReadOnly,
                            FileOwner       = user.UserName
                        };

                        _fileData.Add(file);
                        _fileData.Commit();

                        SendFileNotification(FileOperations.Uploaded, formFile.FileName, user.CompanyId.ToString());
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Upload(UploadViewModel model)
        {
            if ((model != null) && ModelState.IsValid)
            {
                var user = await GetLoggedInUser();

                var containerName = user.CompanyId.ToString().ToLower();
                await _blobService.CreateContainerIfNotExists(containerName);

                if ((model.UploadedFile != null) && (model.UploadedFile.Length > 0))
                {
                    using (var fileStream = model.UploadedFile.OpenReadStream())
                    {
                        await
                        _blobService.UploadBlobFromStream(containerName, model.FileName, fileStream,
                                                          model.UploadedFile.ContentType, false);
                    }

                    var file = new FileInfo
                    {
                        ContentType     = model.ContentType,
                        FileName        = model.FileName,
                        FileContentType = model.UploadedFile.ContentType,
                        FileSizeInBytes = model.UploadedFile.Length,
                        ContainerName   = containerName
                    };

                    _fileData.Add(file);
                    _fileData.Commit();

                    SendFileNotification(FileOperations.Uploaded, model.FileName, user.CompanyId.ToString());

                    return(RedirectToAction(nameof(Details), new { id = file.Id }));
                }
            }
            return(View(model));
        }