ASP.NET Core Upload Asynchronous Upload

1 Answer 258 Views
Upload
Anjali
Top achievements
Rank 2
Anjali asked on 18 Aug 2023, 07:01 PM

Hello,

 

where can I find the controller code for ASP.NET Core Upload Asynchronous Upload. Somehow when I trying to upload my file, I keep getting an error "failed to upload the file". below is what i have in my controller:

 


  public ActionResult Async_Save(IEnumerable<IFormFile> files)
        {
            // The Name of the Upload component is "files"
            string path = Path.Combine(_webHostEnvironment.WebRootPath, "FileSavePath");
            string DestinationPath = _configuration["DestinationPath"].ToString();
            if (files != null)
            {
                MoveFiles mf = new MoveFiles(); 
               
                foreach (var formFile in files)
                {
                    
                  

                    using (var stream = System.IO.File.Create(DestinationPath))
                    {
                        formFile.CopyTo(stream);
                    }

                    
                }
            }

This is my cshtml code:

 

any help will be apprecaited.
@using Kendo.Mvc.UI
<div style="margin-top:60px">
        <div class="block-section">
        @(Html.Kendo().Upload()
            .Name("files")
            .Async(a => a
                .Save("Async_Save", "Upload")
                .Remove("Async_Remove", "Upload")
                .AutoUpload(true)
            )
        )
    </div>
</div>

1 Answer, 1 is accepted

Sort by
0
Accepted
Vasko
Telerik team
answered on 21 Aug 2023, 01:10 PM

Hi Nitu,

When it comes to using an Async Upload Controller,  something like the below code can be of great use for your needs: 

using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Headers;

namespace your_solution_name.Controllers  // Make sure to change the your_solution_name with your actual solution name
{
    public class UploadController : Controller
    {
        public IWebHostEnvironment WebHostEnvironment { get; set; }

        public UploadController(IWebHostEnvironment webHostEnvironment)
        {
            WebHostEnvironment = webHostEnvironment;
        }

        public ActionResult AsyncUpload()
        {
            return View();
        }

        public async Task<ActionResult> Async_Save(IEnumerable<IFormFile> files)
        {
            //! The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

                    //! Some browsers send file names with full path.
                    //! We are only interested in the file name.
                    var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
                    var physicalPath = Path.Combine(WebHostEnvironment.WebRootPath, "App_Data", fileName);

                    //! The files are not actually saved in this demo
                    //using var fileStream = new FileStream(physicalPath, FileMode.Create);
                    //await file.CopyToAsync(fileStream);
                }
            }

            //! Return an empty string to signify success
            return Content("");
        }

        public ActionResult Async_Remove(string[] fileNames)
        {
            //! The parameter of the Remove action must be called "fileNames"

            if (fileNames != null)
            {
                foreach (var fullName in fileNames)
                {
                    var fileName = Path.GetFileName(fullName);
                    var physicalPath = Path.Combine(WebHostEnvironment.WebRootPath, "App_Data", fileName);

                    if (System.IO.File.Exists(physicalPath))
                    {
                        // The files are not actually removed in this demo
                        // System.IO.File.Delete(physicalPath);
                    }
                }
            }

            //! Return an empty string to signify success
            return Content("");
        }
    }
}

 

I tested it in a sample project and from what I can see on my end, it works as intended - when uploading a file, it transfers it to the server with status success. I'm pretty certain this controller will suit your needs very well, just be sure to use the correct method names (Async_Save and Async_Remove) when setting up the Upload component and when you integrate it into your project View, be sure to use the correct Controller name when referencing it in the .Async() method:

@(Html.Kendo().Upload()
            .Name("files")
            .Async(a => a
                .Save("Async_Save", "Upload")           // Talking about these highlighted names pointing to the controller itself 
                .Remove("Async_Remove", "Upload")
                .AutoUpload(true)
            )
        )

  

 

Best regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Upload
Asked by
Anjali
Top achievements
Rank 2
Answers by
Vasko
Telerik team
Share this question
or