Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

jquery - How to rename file if exist using kendoupload?

I am using ChunkUpload of KendoUI Jquery, MVC , I think I need to rename the file before it enters the controller(ChunkSave) but I dont know which part of the process I am able to do that.

My reference for chunk upload: https://demos.telerik.com/kendo-ui/upload/chunkupload?_ga=2.247157956.1737132689.1612402138-1671290078.1573535372

here's my script

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

Controller:

 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);
    }
question from:https://stackoverflow.com/questions/66056214/how-to-rename-file-if-exist-using-kendoupload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you have autoUpload: true, you can't catch duplicate filenames in the front-end. The back-end has to take care of this situation. The back-end could just save it with a different filename (e.g. filename (1).doc).

If you set autoUpload: false, you can catch duplicate filenames by implementing the select event.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...