首页 > 解决方案 > 如果使用 kendoupload 存在,如何重命名文件?

问题描述

我正在使用 KendoUI Jquery,MVC 的 ChunkUpload,我想我需要在文件进入控制器(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);
    }

标签: jqueryasp.net-mvckendo-uikendo-upload

解决方案


既然你有autoUpload: true,你就不能在前端捕获重复的文件名。后端必须处理这种情况。后端可以使用不同的文件名(例如filename (1).doc)保存它。

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


推荐阅读