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

namespace TelerikAspNetCoreApp1.Controllers
{
    public class VendorsController : Controller
    {
        public IWebHostEnvironment WebHostEnvironment { get; set; }

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

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

        public async Task<ActionResult> UploadSave(IEnumerable<IFormFile> uplCtrl)
        {
			// The Name of the Upload component is "uplCtrl"
			if (uplCtrl != null)
            {
                foreach (var file in uplCtrl)
                {
                    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("");
        }
    }
}
