コード例 #1
0
 public async Task <IActionResult> PostLibrary([FromRoute] long idCompany, [FromRoute] long idWeek,
                                               [FromForm(Name = "file")] IFormFile formFile)
 {
     try
     {
         if (formFile != null)
         {
             var directory = Directory.GetCurrentDirectory() + "/storage/wer/";
             if (!Directory.Exists(directory))
             {
                 Directory.CreateDirectory(directory);
             }
             var filePath = directory + Guid.NewGuid() + Path.GetExtension(formFile.FileName);
             if (formFile.Length > 0)
             {
                 using (var stream = new FileStream(filePath, FileMode.Create))
                 {
                     await formFile.CopyToAsync(stream);
                 }
             }
             var file = _filesRepository.createFile(new FileCompanyEntity()
             {
                 Mime      = MimeReader.GetMimeType(Path.GetExtension(formFile.FileName)),
                 Name      = formFile.FileName,
                 Uri       = filePath,
                 Extension = Path.GetExtension(formFile.FileName),
                 WeekId    = idWeek,
                 CompanyId = idCompany,
                 UserId    = HttpContext.UserId().Value
             });
             if (file != null)
             {
                 return(new ObjectResult(file)
                 {
                     StatusCode = 201
                 });
             }
             else
             {
                 System.IO.File.Delete(filePath);
                 return(StatusCode(500));
             }
         }
         else
         {
             return(new BadRequestResult());
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception);
         return(StatusCode(500)); // 500 is generic server error
     }
 }