I have a post to upload file and it is ok for pdf, txt, docx. When I try to upload zip file, I can't extract the files because the file is empty. Some ideas? Thanks in avance. public retOp UploadFile(IFormFile file) { try { string PathFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", Configuration.GetTempDir()); if (!Directory.Exists(PathFile)) { Directory.CreateDirectory(PathFile); } var formFile = file; if (formFile.Length > 0) { string fullPath = Path.Combine(PathFile, file.FileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { formFile.CopyToAsync(stream); } } .....
Asked
Active
Viewed 27 times
2 Answers
0
I Think you should get Formfile from HttpContext.Request
public class UploadController: ControllerBase
{
//this should be your uploadService
private readonly IUploadService _uploadService;
public UploadController(IUploadService uploadService)
{ _uploadService = uploadService; }
[HttpPost("upload")]
public IActionResult Upload()
{
//get file from Request
var formFile = Request.Form.Files[0];
_uploadService.UploadFile(formFile);
}
}

sambath999
- 153
- 1
- 7
-
I'm testing with Swagger and I have some problem to send a file in UI Swagger using Request.Form – maccarilab May 25 '23 at 09:25
0
The problem: the method was not async and I had a "stream.readtimeout". This is the change:
public async Task<retOp> UploadFileAsync(IFormFile file)
{
try
{
....
using (var stream = new FileStream(fullPath,
FileMode.Create))
{
await formFile.CopyToAsync(stream);
} ....
Best regards

maccarilab
- 11
- 1