Telerik Asp.Net Core Widget: Upload 使用
文章目录
一、应用环境
win10+vs2019 community+mysql 8.0.10+Telerik UI for ASP.NET Core R1 2020
二、使用
- 启动vs生成telerik .net core web application,或标准.net core web 应用程序,然后nuget 安装 Telerik UI for ASP.NET Core R1 2020(随安装版本)
- View/Home/index.cshtml,增加以下代码:
@(Html.Kendo().Upload()
.Name("files")
.Async(a => a
.Save("Async_Save", "Upload")
.Remove("Async_Remove", "Upload")
.AutoUpload(true))
)
- 增加Upload控制器,核心代码如下:
public UploadController(IHostingEnvironment hostingEnvironment)
{
HostingEnvironment = hostingEnvironment;
}
public async Task<ActionResult> Async_Save(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);
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName); // The files are not actually saved in this demo
using (var fileStream = new FileStream(physicalPath, FileMode.Create)) //
{
await file.CopyToAsync(fileStream);
}
}
}
return Content("");
}
public ActionResult Async_Remove(string[] fileNames)
{
if (fileNames != null)
{
foreach (var fullName in fileNames)
{
var fileName = Path.GetFileName(fullName);
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);
if (System.IO.File.Exists(physicalPath))
{
System.IO.File.Delete(physicalPath);
}
}
}
return Content("");
}
}
- 注意事项
控件名称files,必须和保存控制器方法的形参同名;
删除控制器方法形参必须为fileNames
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论