如何重命名文件,如果存在使用kendoupload?



我使用的是KendoUI Jquery的ChunkUpload, MVC,我想我需要在它进入控制器(ChunkSave)之前重命名文件,但我不知道我能做到这一点的过程的哪一部分。

我的参考块上传:https://demos.telerik.com/kendo-ui/upload/chunkupload?_ga=2.247157956.1737132689.1612402138-1671290078.1573535372

这是我的脚本

$(".fUploadSingle").kendoUpload({
async: {
chunkSize: 10000000, 
saveUrl: '@Url.Action("ChunkSave", "Streaming", new { area = "" })',
removeUrl: "remove",
autoUpload: true                  
}
});

控制器:

public ActionResult ChunkSave(IEnumerable<HttpPostedFileBase> files, string metaData)
{
if (metaData == null)
{
return Save(files);
}
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(metaData));
var serializer = new DataContractJsonSerializer(typeof(ChunkMetaData));
ChunkMetaData somemetaData = serializer.ReadObject(ms) as ChunkMetaData;
string path = String.Empty;
// The Name of the Upload component is "files"
if (files != null)
{
foreach (var file in files)
{
path = Path.Combine(Server.MapPath("~/Videos/Materials"), somemetaData.FileName);
AppendToFile(path, file.InputStream);
}
}
FileResult fileBlob = new FileResult();
fileBlob.uploaded = somemetaData.TotalChunks - 1 <= somemetaData.ChunkIndex;
fileBlob.fileUid = somemetaData.UploadUid;

return Json(fileBlob);
}

由于您有autoUpload: true,因此无法在前端捕获重复的文件名。后端必须处理这种情况。后端可以用不同的文件名保存它(例如filename (1).doc)。

如果设置了autoUpload: false,可以通过select事件捕获重复的文件名。

相关内容

最新更新