Suppose my breadcrumb is Home > LocalData > WebFile > Tools. When I click the breadcrumb 'WebFile', it navigates to Home.
js:
var firstDataBound = false;
$(document).ready(function () {
$("#FileManager").kendoFileManager({
height: 950,
toolbar: {
items: [
{ name: "search" },
{ name: "createFolder" },
{ name: "upload" },
//{ name: "details" },
{ name: "spacer" },
{ name: "sortDirection" },
{ name: "sortField" },
{ name: "changeView" }
]
},
contextMenu: { // 右鍵選單
items: [
{ name: "rename" },
{ name: "delete" },
{ name: "download", text: "download", command: "download", icon: "k-i-download" } // 自訂
]
},
command: function (e) {
if (e.action == "itemchange" || e.action == "remove") { // 做 rename 或 delete 動作,會導致檔案預覽區被關閉,所以要重新開啟
var filemanager = e.sender;
filemanager.executeCommand({
command: "TogglePaneCommand",
options: { type: "preview" }
});
}
},
uploadUrl: "./handler/FileExploreHandler.ashx?type=upload",
error: function (e) {
alert(e.xhr.responseJSON);
},
dataSource: {
schema: kendo.data.schemas.filemanager,
transport: {
read: {
url: "./handler/FileExploreHandler.ashx",
type: "POST",
dataType: "json",
data: {
"type": "read"
}
},
create: {
url: "./handler/FileExploreHandler.ashx",
type: "POST",
dataType: "json",
data: {
"type": "create"
}
},
update: {
url: "./handler/FileExploreHandler.ashx",
type: "POST",
dataType: "json",
data: {
"type": "update"
}
},
destroy: {
url: "./handler/FileExploreHandler.ashx",
type: "POST",
dataType: "json",
data: {
"type": "destroy"
}
}
}
},
dataBound: function (e) { // 資料綁定完成後,開啟檔案預覽區
if (firstDataBound == false) {
var filemanager = e.sender;
filemanager.executeCommand({
command: "TogglePaneCommand",
options: { type: "preview" }
});
firstDataBound = true;
}
}
});
var filemanagerNS = kendo.ui.filemanager;
filemanagerNS.commands.download = filemanagerNS.FileManagerCommand.extend({
exec: function () {
var selectedFiles = this.filemanager.getSelected();
var file = selectedFiles[0];
var a = document.createElement("a");
a.href = "./handler/FileExploreHandler.ashx?type=download&path=" + encodeURIComponent(file.path);
document.body.appendChild(a);
a.click();
document.body.removeChild(a)
}
});
var fileManager = $("#FileManager").data("kendoFileManager");
fileManager.view("grid");
});
C#:
public void ReadFiles(HttpContext context)
{
try
{
List<FolderFileInfo> FolderFileInfoList = new List<FolderFileInfo>();
string FolderPath = RootPath;
if (!string.IsNullOrEmpty(context.Request.Form["target"]))
{
FolderPath = RootPath + context.Request.Form["target"];
}
foreach (string dir in Directory.GetDirectories(FolderPath))
{
var di = new DirectoryInfo(dir);
FolderFileInfoList.Add(new FolderFileInfo
{
name = di.Name,
isDirectory = true,
hasDirectories = di.GetDirectories().Length > 0,
path = di.FullName.Replace(RootPath, ""),
size = 0,
created = di.CreationTime,
modified = di.LastWriteTime
});
}
foreach (string file in Directory.GetFiles(FolderPath))
{
var fi = new FileInfo(file);
FolderFileInfoList.Add(new FolderFileInfo
{
name = fi.Name.Replace(fi.Extension, ""), // 不能帶副檔名
isDirectory = false,
hasDirectories = false,
path = fi.FullName.Replace(RootPath, ""),
extension = fi.Extension,
size = fi.Length,
created = fi.CreationTime,
modified = fi.LastWriteTime
});
}
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
string json = JsonConvert.SerializeObject(FolderFileInfoList);
context.Response.Write(json);
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
context.Response.Write(JsonConvert.SerializeObject("讀取失敗!"));
}
}
{
public string name { get; set; }
public bool isDirectory { get; set; }
public bool hasDirectories { get; set; }
public string path { get; set; }
public string extension { get; set; }
public long size { get; set; }
public DateTime created { get; set; }
public DateTime modified { get; set; }
}