'Upload File - ASP.NET v6
I want to upload a file on asp.net, but I can't, the data is not saved and the file is not uploaded, when I have filled out all the forms and pressed the submit button, it doesn't react, just stuck on the create page, any solution for my question?
Is my Create.cshtml
<div class="row">
<div class="col-md-4">
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="control-label"></label>
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Genre" class="control-label"></label>
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Rating" class="control-label"></label>
<input asp-for="Rating" class="form-control" />
<span asp-validation-for="Rating" class="text-danger"></span>
</div>
<div class="form-group">
<label for="uploadFile" class="control-label">Clip</label>
<input type="file" name="uploadFile" id="uploadFile" class="form-control" />
<span id="uploadFile" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
I've added an element to add a file with the name fileUpload, like that.
my model :
public class Movie
{
public int Id { get; set; }
[StringLength(60, MinimumLength = 3)]
public string Title { get; set; }
[Display(Name = "Release Date"), DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z\s]*$"), Required, StringLength(30)]
public string Genre { get; set; }
[Range(1, 100), DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$"), StringLength(5)]
public string Rating { get; set; }
[Required]
public string Clip { get; set; }
}
and my controller :
// GET: Movie/Create
public IActionResult Create()
{
return View();
}
// POST: Movie/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Title,ReleaseDate,Genre,Price,Rating,uploadFile")] Movie movie, IFormFile uploadFile)
{
if (ModelState.IsValid)
{
if (uploadFile != null && uploadFile.Length > 0)
{
var getTime = DateTime.UtcNow.ToString("ddMMyyHHmmss");
var fileName = getTime + Path.GetFileName(uploadFile.FileName);
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets", fileName);
movie.Clip = filePath;
_context.Add(movie);
await _context.SaveChangesAsync();
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await uploadFile.CopyToAsync(fileStream);
}
return RedirectToAction(nameof(Index));
}
}
return View(movie);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
