Telerik Asp.Net Core Widget: Upload 使用

一、应用环境
win10+vs2019 community+mysql 8.0.10+Telerik UI for ASP.NET Core R1 2020
二、使用

  1. 启动vs生成telerik .net core web application,或标准.net core web 应用程序,然后nuget 安装 Telerik UI for ASP.NET Core R1 2020(随安装版本)
  2. View/Home/index.cshtml,增加以下代码:

@(Html.Kendo().Upload()

.Name("files")  
.Async(a => a 
    .Save("Async_Save",  "Upload")  
    .Remove("Async_Remove",  "Upload")  
    .AutoUpload(true))  

)

  1. 增加Upload控制器,核心代码如下:

public UploadController(IHostingEnvironment hostingEnvironment)
{

HostingEnvironment  = hostingEnvironment;  

}
public async Task Async_Save(IEnumerable 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("");  
}  

}

  1. 注意事项

控件名称files,必须和保存控制器方法的形参同名;
删除控制器方法形参必须为fileNames

你可能感兴趣的:(html5)