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

Upload file save to database

$
0
0

Hi folks,

I am using asp.net 5 (MVC 6) using beta 8. I am having problem with upload file and save to a database.

Here is my sample code:

View:

<form asp-controller="Home" asp-action="Create" method="post" enctype="multipart/form-data"><div class="form-inline"><label asp-for="Name">Name:</label><input asp-for="Name" type="text" class="form-control" placeholder="Name" /><label asp-for="FilePath">Attachment :</label><input asp-for="FilePath" type="file" name="files" id="files" class="form-control" /><input type="submit" class="btn btn-default" value="Create" /></div></form>

Model:

    public class ApplicantName
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string FilePath { get; set; }
    }

Controller:

        [HttpPost]
        public async Task<ActionResult> Create([Bind("Name", "FilePath")] ApplicantName applicantName, ICollection<IFormFile> files )
        {

            var uploads = Path.Combine(environment.WebRootPath, "Uploaded");

            foreach (var file in files)
            {
                if (file != null && file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    await file.SaveAsAsync(Path.Combine(uploads, fileName)); 
                }
            }

            context.ApplicantNames.Add(applicantName);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

I can see save as file in my web root folder but cannot see store file in database. What am i missing?
I am waiting for your response.

Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles



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