Hi!
I have tried following the Asynchronous upload documentation, but whenever I try to upload file(s), it always fails and the server responded with a status of 404.
Thanks in advance for your help.
My code is as follows:
HTML:
<div>
@(Html.Kendo().Upload()
.Name("files")
.Async(a => a
.Save("SaveAsync", "Home")
.Remove("RemoveAsync", "Home")
.AutoUpload(true)
)
)
</div>
Controller:
public class HomeController : Controller
{
private IWebHostEnvironment _WebHostEnvironment;
public HomeController(IWebHostEnvironment webHostEnvironment)
{
_WebHostEnvironment = webHostEnvironment;
}
public async Task<ActionResult> SaveAsync(IEnumerable<IFormFile> files)
{
// The Name of the Upload component is "files".
if (files != null)
{
foreach (var file in files)
{
var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var physicalPath = Path.Combine(_WebHostEnvironment.WebRootPath, "App_Data", fileName);
using (var fileStream = new FileStream(physicalPath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
// Return an empty string to signify success.
return Content("");
}
public ActionResult RemoveAsync(string[] fileNames)
{
// The parameter of the Remove action must be called "fileNames".
if (fileNames != null)
{
foreach (var fullName in fileNames)
{
var fileName = Path.GetFileName(fullName);
var physicalPath = Path.Combine(_WebHostEnvironment.WebRootPath, "App_Data", fileName);
// TODO: Verify user permissions.
if (System.IO.File.Exists(physicalPath))
{
System.IO.File.Delete(physicalPath);
}
}
}
// Return an empty string to signify success.
return Content("");
}
}