Beispiel #1
0
        public async Task <IActionResult> Add(AddViewModel addViewModel, int id)
        {
            if (!ModelState.IsValid)
            {
                return(View(addViewModel));
            }

            Stream htmlstream = await HtmlHelper.GetHtmlAsStream(addViewModel.HttpUrl); //爬取html文件流

            var filename = Path.Combine(_e.WebRootPath, addViewModel.FileName);         //保存到本地

            using (var stream = new FileStream(filename, FileMode.Create))
            {
                await htmlstream.CopyToAsync(stream);

                var file = new File()
                {
                    FileName   = addViewModel.FileName,
                    Url        = addViewModel.FileName,
                    FileTypeId = id
                };
                var result = await _fileService.AddAsync(file);

                if (result > 0)//说明添加成功
                {
                    return(RedirectToAction(nameof(Index), "FileType"));
                }
            }

            return(View(addViewModel));
        }
        public async Task <IActionResult> Upload(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest());
            }

            var uploadFile = file.ConvertToFileInfoModel();
            await _fileService.AddAsync(uploadFile);

            return(NoContent());
        }
        public async Task <object> DownloadFile(IFormFile file)
        {
            var resultStream = new MemoryStream();

            await file.CopyToAsync(resultStream);

            FileModel model = new FileModel
            {
                NameFile   = file.FileName,
                TypeFile   = file.ContentType,
                StreamFile = resultStream
            };

            return(await _service.AddAsync(model));
        }
 public async Task HandleAsync(CreateFile command)
 {
     _logger.LogInformation($"Creating file: '{command.Id}' for user: '******'.");
     try
     {
         await _fileService.AddAsync(command.UserId,
                                     new BasketFile(command.Name, command.Path, command.Extension));
     }
     catch (ActioException e)
     {
         _logger.LogError(e, e.Message);
         throw;
     }
     catch (Exception e)
     {
         _logger.LogError(e, e.Message);
     }
 }
Beispiel #5
0
        public async Task <IActionResult> UploadSingleFile([FromForm] FileResource fileResource)
        {
            if (fileResource.File == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed posting file.",
                    Detail   = "File is null.",
                    Instance = "ACD46F17-A239-4353-92A5-0B81AA0A96E9"
                };
                return(BadRequest(problem));
            }
            try
            {
                DateTime uploadDateTime = DateTime.Now;
                int      fileExtPos     = fileResource.File.FileName.LastIndexOf(".");
                string   extension      = fileResource.File.FileName.Substring(fileExtPos);
                string   newFileName    = Guid.NewGuid() + extension;
                User     user           = await HttpContext.GetContextUser(userService)
                                          .ConfigureAwait(false);

                File file = new File(newFileName, newFileName, user, uploadDateTime);

                await fileUploader.CopyFileToDirectory(fileResource.File, newFileName);

                await fileService.AddAsync(file);

                fileService.Save();

                return(Ok(mapper.Map <File, FileResourceResult>(file)));
            } catch (FileExistException fileExistException)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = fileExistException.Message,
                    Detail   = "Please rename filename.",
                    Instance = "D902F8C6-23FF-4506-B272-C757BD709464"
                };
                return(BadRequest(problem));
            }
        }
        public async Task CreateFile_Success()
        {
            var mass       = new byte[] { 0, 1, 2, 3, 4, 5 };
            var expectFile = new FileModel
            {
                NameFile   = "Test",
                StreamFile = new MemoryStream(mass),
                TypeFile   = "jpg"
            };

            //Act
            var resultId = await _service.AddAsync(expectFile);

            var resuldFile = await _context.Files.SingleOrDefaultAsync(x => x.FileId == resultId);

            var resultStoreFile = _filesStore.SingleOrDefault(x => x == resultId.ToString());

            //Assert
            Assert.AreEqual(expectFile.NameFile, resuldFile.NameFile);
            Assert.AreEqual(expectFile.TypeFile, resuldFile.TypeFile);
            //Такое сравнение, потому что в моке при добавление записываем тип длину(для примера), а название это Id
            Assert.AreEqual(resultId.ToString(), resultStoreFile);
        }
Beispiel #7
0
        public async Task <ActionResult <List <SheetAdmissionValidationErrors> > > PostFile(File file)
        {
            _context.File.Add(file);

            //Lê o arquivo EXCEL
            var datatable2 = _excelServices.ReadSheet(file.Data, 0);

            ActionResult <List <SheetAdmissionValidationErrors> > _lsheetAdmissionValidationErrorRetorno = new List <SheetAdmissionValidationErrors>();
            List <SheetAdmissionValidationErrors> _lsheetAdmissionValidationError = new List <SheetAdmissionValidationErrors>();
            List <SheetAdmission> ValidItems = new List <SheetAdmission>();
            //Valida o Arquivo de Adimissão
            var isValid = _sheetAdmissionValidation.Validate(datatable2, ref _lsheetAdmissionValidationError, ref ValidItems);

            _lsheetAdmissionValidationErrorRetorno = _lsheetAdmissionValidationError;

            if (!isValid)
            {
                //Retorna os itens do arquivo para correção
                return(Accepted(_lsheetAdmissionValidationError));
            }
            else
            {
                //Grava Arquivo
                var _file = await _fileService.AddAsync(file);

                _file.DateRegister = DateTime.Today;
                foreach (SheetAdmission lSheetAdmission in ValidItems)
                {
                    //Grava os itens do Arquivo
                    lSheetAdmission.FileId = _file.Id;
                    await _sheetAdmissionService.AddAsync(lSheetAdmission);
                }
            }

            return(CreatedAtAction("GetFile", new { id = file.Id }, file));
        }
 public Task <IEnumerable <BinaryFile> > AddAsync() => _fileService.AddAsync(_directory, Request.Files());
Beispiel #9
0
        public async Task <FileView> Post(int torrentId, [FromForm] IFormFile file)
        {
            var createdFile = await _fileService.AddAsync(torrentId, file);

            return(_mapper.Map <FileView>(createdFile));
        }