jquery - 如果使用 kendoupload 存在,如何重命名文件?
问题描述
我正在使用 KendoUI Jquery,MVC 的 ChunkUpload,我想我需要在文件进入控制器(ChunkSave)之前重命名文件,但我不知道我能够做到这一点的过程的哪一部分。
这是我的脚本
$(".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
推荐阅读
- python - Python:捕获对内存中文件的所有写入
- java - 如何使用 XPAGES DesignerFacesServlet 显示泰语字母(和另一个亚洲/阿拉伯字母)
- javascript - 从日期获取多少天/小时/秒前
- javascript - Nativescript vue登录函数返回true
- c++ - googlemock - 如何处理“多重定义”问题?
- python - 如何从 Python 中的字符串中删除特定的部分/片段
- python - Keras fit_generator(datagen.flow(X_train)) 函数拍摄的照片比 X_train 少?
- .net - 无法引用 Nuget 包中包含的 dll
- regex - 在 Python 3 中,当替换字符串包含 unicode 字符时,如何使用 Regex 替换文件中的字符串
- python - 如何为具有数字参数的函数返回 True 或 False?