This is a migrated thread and some comments may be shown as answers.

Delete unfinished file on Cancel

3 Answers 20 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 10 Aug 2020, 06:18 PM
When the user clicks Cancel, the filename is removed from the list on the page, but I noticed the actual file fragment still exists on the server.  Is there any way to also delete the cancelled file from the file system?  I tried adding removeFileByUid() to the onCancel function, but I get the following JavaScript error:
Uncaught ReferenceError: upload is not defined
    at init.onCancel ((index):96)
    at init.trigger (kendo.all.js:164)
    at init._onFileAction (kendo.all.js:71201)
    at HTMLButtonElement.d (jquery.min.js:2)
    at HTMLDivElement.dispatch (jquery.min.js:3)
    at HTMLDivElement.r.handle (jquery.min.js:3)
onCancel    @   (index):96
trigger @   kendo.all.min.js:25
_onFileAction   @   kendo.all.js:72295
d   @   jquery.min.js:2
dispatch    @   jquery.min.js:3
r.handle    @   jquery.min.js:3

I assume this is because my removeFileByUid() call runs before the built-in "Cancel" code, which is also trying to remove the file that was already removed...?

I also tried making an Ajax call to my C# "remove" function directly, and I get the following error: "The process cannot access the file...because it is being used by another process."

For reference, here is my uploader:
@(Html.Kendo().Upload()
    .Name("files")
    .Async(a => a
        .Save("Chunk_Upload_Save", "Home")
        .Remove("Async_Remove", "Home")
        .AutoUpload(true)
        .ChunkSize(1024000) //bytes
    )
    .Events(events => events
        .Cancel("onCancel")
    )
)


3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 12 Aug 2020, 04:37 PM

Hello Andrew,

In the Cancel event you have the e.files array. In order to ensure that none of the canceled files will be left on the server, you can send an AJAX request to the Remove action method with the e.files as additional data. In the action method, you can iterate the array, and remove the files:

public ActionResult Remove(string[] fileNames)
{
    // The parameter of the Remove action must be called "fileNames"

    if (fileNames != null)
    {
        foreach (var fullName in fileNames)
        {
            var fileName = Path.GetFileName(fullName);
            var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);

            // TODO: Verify user permissions

            if (System.IO.File.Exists(physicalPath))
            {
                // The files are not actually removed in this demo
                System.IO.File.Delete(physicalPath);
            }
        }
    }

    // Return an empty string to signify success
    return Content("");
}

I hope I was helpful. If the above does not resolve the problem, please send me a small runnable example where I can test it, and I will gladly assist you.

Regards,
Martin
Progress Telerik

0
Andrew
Top achievements
Rank 1
answered on 12 Aug 2020, 04:50 PM

Hi Martin,

Yes, I had the same idea.  But when I tried it, I got the error, "The process cannot access the file...because it is being used by another process."  I'm guessing the file is still locked by the write process, due to the asynchronous call...? I am also using the same Remove() code that you posted.

0
Martin
Telerik team
answered on 14 Aug 2020, 10:16 AM

Hello Andrew,

Could you please share a runnable example where I can observe the error? I was not able to reproduce it based on the information provided so far. I will then be able to investigate what is causing the behavior.

Looking forward to your reply.

Regards,
Martin
Progress Telerik

Tags
Upload
Asked by
Andrew
Top achievements
Rank 1
Answers by
Martin
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or