0

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); } } .....

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm testing with Swagger and I have some problem to send a file in UI Swagger using Request.Form
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.