Net Core Upload File
Desine View
<form enctype="multipart/form-data" method="post">
<div>
<label for="f">Image</label> <div>
<input type="file" name="f" id="f">
</div>
</div>
<div>
<button>Upload</button>
</div>
</form>
Upload File
public IActionResult Simple() => View();
[HttpPost]
public IActionResult Simple(IFormFile f){
if(f != null){
string ext = Path.GetExtension(f.FileName);
string fileName = Helper.RandomString(32 - ext.Length) + ext;
Image obj = new Image{
Name = f.FileName,
Length = f.Length,
ImageUrl = fileName,
Type = f.ContentType
};
string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", fileName);
using(Stream stream = new FileStream(path, FileMode.Create)){
f.CopyTo(stream);
}
return Json(obj);
}
return Json(null);
}