I have setup kendo upload as below.
@(Html.Kendo().Upload().Name("files")
.Async(a => a
.Save("UploadAttachments", "Mycontroller")
.Remove("RemoveAttachments", "Mycontroller")
.SaveField("files")
.RemoveField("ids")
.AutoUpload(true))
.Events(e => e
.Success("onUploadSuccess")
.Error("onUploadError"))
.Files(files =>
{
foreach (var f in Model.Attachments)
{
files.Add().Name(f.FileName).Extension(Path.GetExtension(f.FileName));
}
}))
UploadAttachments methods saves the file in server file and create a record in db and returns the following model
Id-long
FileName: fileName
RemoveAttachments methods expects ids. During create with empty existing files I have following event handler which updates the file so that I can be deleted using id.
function onUploadSuccess(e) {
if (e.operation == "upload") {
e.files[0].data = e.response[0]; // hold the attachment detail
e.files[0].name = e.response[0].Id; // change name with id so it can be passed to remove method
}
}
But with existing files, I couldn't think a way of updating the file or assign the id from Model.Attachements into upload control.
I can set name with id during init, but in that way I can't show the name of file correctly.
foreach (var f in Model.Attachments)
{
files.Add().Name(f.Id.ToString).Extension(Path.GetExtension(f.FileName));
}
This will show wrong file name in UI.