File Manager: Open file on double click

2 Answers 689 Views
FileManager
Raimo
Top achievements
Rank 1
Iron
Raimo asked on 07 Jul 2021, 03:14 PM

I have implemented the File Manager like in the Demo and I have added the Download in the ContextMenu.

https://demos.telerik.com/aspnet-mvc/filemanager

https://docs.telerik.com/aspnet-mvc/knowledge-base/filemanager-create-download-command

so fare so good.

 

Now when I double click on a folder I get into the folder, but when I double click on a file nothing happens.

How can I implement that the file is open (downloaded) on double click?

 

In the Demo (https://demos.telerik.com/aspnet-mvc/filemanager) ist looks like the same behavior.

2 Answers, 1 is accepted

Sort by
1
Accepted
Ivan Danchev
Telerik team
answered on 12 Jul 2021, 09:15 AM

Hello Raimo,

The Open event can be used to detect a double click on a file in the FileManager. In the event handler you can add some custom logic that can either:

1. Show a preview of the file (if it is an image). See this demo that demonstrates this scenario: https://demos.telerik.com/aspnet-mvc/filemanager/image-preview

or

2. Download the file. The implementation of the event handler is similar to that in the linked demo:

Attach the handler:

.Events(events => events
        .Open("onOpen")
    )

In the handler get the file path and pass it to a Download action to download it:

<script>
    function onOpen(e) {
        if (e.entry.extension == ".png" || e.entry.extension == ".jpg") {
            window.location = '/FileManager/Download?path=' + e.entry.path;
        }
    }
</script>

The Download action is the same as in the article you linked:

[HttpGet]
public FileResult Download(string path)
{
    var virtualPath = "~/Content/UserFiles/Folders/" + path;
    var filePath = HostingEnvironment.MapPath(virtualPath);
    FileInfo file = new FileInfo(filePath);

    System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
    {
        FileName = file.Name,
        Inline = false
    };
    Response.Headers.Add("Content-Disposition", cd.ToString());
    Response.Headers.Add("X-Content-Type-Options", "nosniff");

    string contentType = MimeMapping.GetMimeMapping(file.Name);
    var readStream = System.IO.File.ReadAllBytes(filePath);
    return File(readStream, contentType);
}

Regards,
Ivan Danchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Raimo
Top achievements
Rank 1
Iron
answered on 14 Jul 2021, 09:48 AM

Thx! I changed it a little and now it works perfect for me.


    function onOpen(e) {
        if (!e.entry.isDirectory) {
            window.location = '/FileManager/Download?path=' + e.entry.path;
        }
    }

Ivan Danchev
Telerik team
commented on 19 Jul 2021, 08:54 AM

Yes, this is the correct condition for checking whether the double click is over a file or a directory. The one I posted is copied from the image preview demo, where it checks for 2 specific file extensions.
Tags
FileManager
Asked by
Raimo
Top achievements
Rank 1
Iron
Answers by
Ivan Danchev
Telerik team
Raimo
Top achievements
Rank 1
Iron
Share this question
or