Jackington
Jackington

Reputation: 127

File Upload Return

I'm new to .NET Core MVC and trying to make an application that uploads a file, I've managed to do that but when it uploads the file it takes you to the file count and size whereas I just want it to remain on the same page does anybody know how I can do this?

  [HttpPost("UploadFiles")]
    public async Task<IActionResult> FileUpload(List<IFormFile> files)
    {
        var webroot = _env.WebRootPath;
        long size = files.Sum(f => f.Length);
        var filePaths = new List<string>();
        foreach (var formFile in files)
        {
            if(formFile.Length > 0)
            {                   
                var filePath = Path.Combine(/*AppContext.BaseDirectory*/webroot, $"{Guid.NewGuid().ToString()}.txt"); /*Path.GetTempPath()+ Guid.NewGuid().ToString()+".txt";*/
                filePaths.Add(filePath);
                TempData["filepath"] = filePath;

                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    await formFile.CopyToAsync(stream);                        

                }
            }
        }
        return Ok(new { count = files.Count, size, filePaths });

Any suggestions would be much appreciated

Upvotes: 0

Views: 146

Answers (2)

Saravanakumar
Saravanakumar

Reputation: 28

Not able to add as comment... We can use partial view concept, people use this concept to avoid this situation. Else we can also use IFrame.

I'll prefer to go with partial view concept ease to handle multiple event and scenario.

Happy coding

Upvotes: 0

Dileep Sreepathi
Dileep Sreepathi

Reputation: 254

You can do using the ajax calling for this, submit the form using the Ajax call

Upvotes: 0

Related Questions