Mvc Core File Upload Example

a

All Code

<form asp-controller=”Order” asp-action=”ImportExcelCargo” method=”post” enctype=”multipart/form-data”>

@*<antiforgery-token />*@

<em>Siparişlere ait kargo durumu güncellemeye yarar</em>

<div class=”form-group”>

&nbsp;

</div>

<div class=”form-group”>

<div class=”col-md-2" style=”margin-top: 2px; height: 34px; padding-left: 25px;”>

<label class=”control-label”>

Dosya Seçiniz

</label>

</div>

<div class=”col-md-10">

<input required type=”file” id=”file” name=”file” />

</div>

</div>

<div class=”form-group”>

<div class=”col-md-2">

&nbsp;

</div>

<div class=”col-md-10 text-right mt-10">

<input type=”submit” class=”k-button” value=”@T(“Admin.Common.ImportFromExcel”)” />

</div>

</div>

</form>

[HttpPost]

public IActionResult ImportExcelCargo(IFormFile file)

{

string path = Path.Combine(“Content/” + file.Name);

if (!Directory.Exists(path))

{

Directory.CreateDirectory(path);

}

using (FileStream stream = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))

{

file.CopyTo(stream);

}

return RedirectToAction(“List”, “Order”);

}