Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

1.0.0-Beta7 File Upload Issue

$
0
0

I am not sure If I am posting this question in the right place or not...

I am using the latest version of ASP.Net 5 1.0.0-Beta7 and MVC 6.0.0 Beta7. I need to upload a file and then process that file but am having issues with passing the file from the view to the controller.

Here is my View:

@{
    ViewBag.Title = "Upload";
}
@model Upload<form asp-controller="Upload" asp-action="Upload" method="post"><div class="container-fluid"><div class="row"><br /></div><div class="row"><div class="panel panel-default col-md-6 col-md-offset-3"><div class="row"><div class="col-md-6"><div class="input-group"><span class="input-group-btn"><span class="btn btn-primary btn-file">
                                    Browse&hellip; @Html.TextBoxFor(m => m.fileToUpload, new { type = "file" })</span></span><input type="text" class="form-control" readonly></div></div><div class="col-md-4"></div><div class="col-md-2"><input type="submit" value="Upload" /></div></div><div class="row"><br /></div></div></div></div></form>

Here is my Model:

public class Upload
    {
        public string fileName { get; set; }
        public string fileType { get; set; }
        [Required]
        public IFormFile fileToUpload { get; set; }
    }

Here is my Controller:

public class UploadController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public UploadController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
        // Initial get of the upload page
        [HttpGet]
        public IActionResult Index()
        {
            return View(new Upload());
        }

        [HttpPost]
        public IActionResult Upload(Upload fileIn)
        {
            if (ModelState.IsValid)
            {
                //var file = uploadFile;
                var parsedContent = ContentDispositionHeaderValue.Parse(fileIn.fileToUpload.ContentDisposition);
                var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", parsedContent.FileName.Trim('"'));
                fileIn.fileToUpload.SaveAsAsync(fileName);
                string test = Path.GetExtension(fileName);
                if (Path.GetExtension(fileName) == ".xlsx")
                {
                    LoadXLSX(fileName);
                }
                else if (Path.GetExtension(fileName) == ".csv")
                {
                    LoadCSV(fileName);
                }
                else
                {

                }
            }

            return View("Index");
        }
}

According to everything I have read this should work fine and the file should be contained in the returned Model object. When I set a breakpoint in the upload controller on the ModelState.IsValid line and look at the fileIn object the fileToUpload property is null. What am I doing wrong?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>