Hi,
Trying to upload file unsuccessfull. Any one can help me please? I am using Net.Core 2.0
Here my model UploadFile.cs:
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Internal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace PalipiInnovation.Entities { public class UploadFile { public int Id { get; set; } [Required, StringLength(100)] public string Name { get; set; } [Display(Name = "File Path")] public string FilePath { get; set; } [Display(Name = "Thumbnail Path")] public string ThumbNailPath { get; set; } [Display(Name = "Url")] public string UrlPath { get; set; } [Required] [NotMapped] [Display(Name = "Document File")] public FormFile DocumentFile { get; set; } } }
Here my Create Action:
public IActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("Id,Name,FilePath,ThumbNailPath,UrlPath,DocumentFile")] UploadFile uploadFile) { if (ModelState.IsValid) { uploadFile.FilePath = uploadFile.DocumentFile.FileName; await UploadDocument(); _context.Add(uploadFile); await _context.SaveChangesAsync(); Message = $"Upload document {UploadFile.FilePath} has been successfully!"; return RedirectToAction(nameof(Index)); } return View(uploadFile); }
Here my View Create.cshtml:
@model PalipiInnovation.Entities.UploadFile @{ ViewData["Title"] = "Create"; }<div class="panel panel-default container" style="margin-top:25px;"><div class="panel-heading panel-title">@ViewData["Title"]</div><div class="panel-body"><div class="row"><div class="col-md-4"><form method="post" asp-action="Create" enctype="multipart/form-data"><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group-sm"><label asp-for="Name" class="control-label"></label><input asp-for="Name" class="form-control" /><span asp-validation-for="Name" class="text-danger"></span></div><div class="form-group-sm"><label asp-for="DocumentFile" class="col-md-3 control-label"></label><div class="col-md-9"><input asp-for="DocumentFile" type="file" class="form-control" style="height:auto" /><span asp-validation-for="DocumentFile" class="text-danger"></span></div></div><div class="form-group-sm col-md-offset-3"><input type="submit" value="Create" class="btn btn-success" style="margin-top:15px" /></div></form><a asp-action="Index" class="btn btn-primary col-md-offset-3">Back to List</a></div></div></div></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Thanks guys
Jannen Siahaan