コード例 #1
0
 public async Task <IActionResult> UploadFileReport([FromRoute(Name = "idCompany")] long idCompany, [FromRoute(Name = "idReport")] long idReport,
                                                    [FromRoute(Name = "filetype")] Int16 filetype,
                                                    [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 FileEntity()
             {
                 Mime     = MimeReader.GetMimeType(Path.GetExtension(formFile.FileName)),
                 Name     = formFile.FileName,
                 Uri      = filePath,
                 Type     = filetype,
                 ReportId = idReport
             });
             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
     }
 }