public async Task <IActionResult> Upload([FromForm] CreateFileModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var uploads = Path.Combine(hostingEnvironment.ContentRootPath, "uploads");

            if (!Directory.Exists(uploads))
            {
                Directory.CreateDirectory(uploads);
            }

            if (model.File.Length > 0)
            {
                var filePath = Path.Combine(uploads, model.File.FileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await model.File.CopyToAsync(fileStream);
                }
            }
            var file = new Common.Entity.FileService.File();

            file.DownloadDate = DateTime.Now;
            file.FileAccessId = Convert.ToInt32(model.AccessId);
            file.CategoryId   = Convert.ToInt32(model.CategoryId);
            file.Description  = model.Description;
            file.Name         = model.File.FileName;
            file.Size         = model.File.Length;

            var fileUrl = new FileUrl();

            fileUrl.Url = uploads;

            var newFileUrl = db.FileUrls.Create(fileUrl);

            if (newFileUrl == null)
            {
                return(BadRequest());
            }

            file.UserId    = 1;
            file.FileUrlId = newFileUrl.Id;

            FileCard newFile = db.Files.Create(file);

            if (newFile == null)
            {
                return(BadRequest());
            }

            return(Ok(newFile));
        }
예제 #2
0
        public IActionResult Put(int id, [FromBody] Common.Entity.FileService.File model)
        {
            if (model == null || id != model.Id)
            {
                return(BadRequest());
            }

            db.Files.Update(model);

            return(Ok(model));
        }