Hi,
If a user right-clicks over a file in File Manager, one of the context menu options available is to "open" said file. However, on all files it seems like the "open" function doesn't seem to do anything? What is the intended purpose of this option?
Also, speaking of opening files, is it feasible to open files in File Manager? Given the requirement of when a user double clicks on a file, it will open said file on another browser tab. For instance, uploaded PDF and HTML files can be opened on another browser tab to be viewed by the user. For other files that can't be viewed on the browser, the option of downloading said files will instead be triggered. If proved to be feasible, can you provide some sample code where this requirement is fully displayed?
Thanks!
|
<div class="control-section">
<SfFileManager TValue="FileManagerDirectoryContent" ID="filemanager" @ref="file" View="ViewType.Details">
<FileManagerEvents TValue="FileManagerDirectoryContent" OnFileOpen="fileOpen"></FileManagerEvents>
<FileManagerAjaxSettings Url="/api/FileManager/FileOperations"
UploadUrl="/api/FileManager/Upload"
DownloadUrl="/api/FileManager/Download"
GetImageUrl="/api/FileManager/GetImage">
</FileManagerAjaxSettings>
</SfFileManager>
<SfDialog @ref="dialog" Width="1200px" ShowCloseIcon="true" IsModal="true" Visible="false" Header="PDF Viewer">
<DialogTemplates>
<Content>
<SfPdfViewerServer DocumentPath="@DocumentPath" Width="1180px" Height="500px" />
</Content>
</DialogTemplates>
</SfDialog>
</div>
@code {
SfFileManager<FileManagerDirectoryContent> file;
public string DocumentPath { get; set; }
SfPdfViewerServer PdfViewer;
SfDialog dialog;
public void fileOpen(Syncfusion.Blazor.FileManager.FileOpenEventArgs<FileManagerDirectoryContent> args)
{
if ((args.FileDetails.IsFile == true) && args.FileDetails.Type == ".pdf")
{
dialog.Show();
DocumentPath = "wwwroot/Files" + args.FileDetails.FilterPath + args.FileDetails.Name;
}
}
} |
Hi Sowmiya,
Thank you for the response.
As much as possible we wanted to open the files on another browser tab, not on the same page as the File Manager component using a PDF viewer or the Document Editor. However, I think this is a good reference to have if our client wants to go on that route. But what we wanted for now, is to open a file normally on the browser if the file can be opened to the browser, for example a PDF file, or an HTML file.
You've mentioned that it doesn't have the capability to open the files, but you have provided the custom code to handle the file open event. Can you provide a sample code that we can substitute in that fileOpen method where if we open a PDF/HTML file, it will be displayed on the browser. And if the file can't be opened in a browser, it will download the file instead?
Thanks!
|
<div class="control-section">
<SfFileManager TValue="FileManagerDirectoryContent" ID="filemanager" @ref="file" View="ViewType.Details">
<FileManagerEvents TValue="FileManagerDirectoryContent" OnFileOpen="fileOpen"></FileManagerEvents>
<FileManagerAjaxSettings Url="/api/FileManager/FileOperations"
UploadUrl="/api/FileManager/Upload"
DownloadUrl="/api/FileManager/Download"
GetImageUrl="/api/FileManager/GetImage">
</FileManagerAjaxSettings>
</SfFileManager>
</div>
@code {
SfFileManager<FileManagerDirectoryContent> file;
public async Task fileOpen(Syncfusion.Blazor.FileManager.FileOpenEventArgs<FileManagerDirectoryContent> args)
{
if ((args.FileDetails.IsFile == true))
{
string[] fileName = { args.FileDetails.Name };
await file.DownloadFilesAsync(fileName);
}
}
} |
Hi, sorry to interrupt this thread.
The "fileOpen" is not working in the sample ending 1670. It will hit the function when a folder is onclicked however, "open" in the context menu will not trigger the "fileOpen" function.
|
<SfFileManager TValue="FileManagerDirectoryContent" ID="filemanager" @ref="file" View="ViewType.Details">
<FileManagerEvents TValue="FileManagerDirectoryContent" OnFileOpen="fileOpen" OnMenuClick="menuClick"></FileManagerEvents>
<FileManagerAjaxSettings Url="/api/FileManager/FileOperations"
UploadUrl="/api/FileManager/Upload"
DownloadUrl="/api/FileManager/Download"
GetImageUrl="/api/FileManager/GetImage">
</FileManagerAjaxSettings>
</SfFileManager>
...
public void menuClick(MenuClickEventArgs<FileManagerDirectoryContent> args)
{
if (args.Item.Text == "Open" && (args.FileDetails[0].IsFile == true) && args.FileDetails[0].Type == ".pdf")
{
dialog.Show();
DocumentPath = "wwwroot/Files" + args.FileDetails[0].FilterPath + args.FileDetails[0].Name;
}
} |
How can this be implemented for angular File manager control ?
Hi Satadal,
We can understand that you are trying to open the PDF files in the File Manager component in Angular platform. We have achieved your requirement by using fileOpen, menuClick events and getFileStream method. We have created a sample aligning with your requirement. Kindly refer the code changes below,
|
[app.component.html]
<ejs-filemanager id='overview' OnMenuClick="menuClick" height="700px" [ajaxSettings]='ajaxSettings' (fileOpen)="onFileOpen($event)" [toolbarSettings]="toolbarSettings" [contextMenuSettings]="contextMenuSettings" [view]='view'> </ejs-filemanager> <ejs-dialog #dialogInstance width="1200px" class="pdf-container" [showCloseIcon]="true" [isModal]="true" [visible]="false" header="PDF Viewer"> <ejs-pdfviewer id="pdfViewer" width="1180px" height="500px"></ejs-pdfviewer> </ejs-dialog> |
|
[app.component.ts]
public onFileOpen(args: any) { if ((args.fileDetails.isFile == true) && args.fileDetails.type == ".pdf") { this.dialogObj.show(); this.filePath = args.fileDetails.filterPath.replace(/\\/g, "/") + args.fileDetails.name; this.getFileStream(this.filePath) } }
public menuClick(args: any) { if (args.item.text == "Open" && (args.fileDetails[0].isFile == true) && args.fileDetails[0].type == ".pdf") { this.dialogObj.show(); this.filePath = args.fileDetails.filterPath.replace(/\\/g, "/") + args.fileDetails.name; this.getFileStream(this.filePath) } }
getFileStream(filePath: string) { let ajax: XMLHttpRequest = new XMLHttpRequest(); ajax.open("POST", this.hostUrl + "api/FileManager/GetDocument", true); ajax.setRequestHeader("content-type", "application/json");
ajax.onreadystatechange = () => { if (ajax.readyState === 4) { if (ajax.status === 200 || ajax.status === 304) { var pdfviewer = (<any>document.getElementById('pdfViewer')).ej2_instances[0]; pdfviewer.load(ajax.responseText, null); } } }; ajax.send(JSON.stringify({ "FileName": filePath, "Action": ("LoadPDF") })); } |
You need to write the corresponding server side in operation on opening the files in server side.
|
[Route("GetDocument")] public string GetDocument([FromBody] CustomParams param) { string path = this.basePath + "\\wwwroot\\Files" + (param.FileName).Replace("/", "\\"); //for PDF Files var docBytes = System.IO.File.ReadAllBytes(path); //we can convert the document stream to bytes then convert to base64 string docBase64 = "data:application/pdf;base64," + Convert.ToBase64String(docBytes); return (docBase64); } |
Service provider documentation link: https://ej2.syncfusion.com/angular/documentation/file-manager/file-system-provider#aspnet-core-file-system-provider
We have also attached the sample for your reference.
Check out the shared details and get back to us if you need further assistance.
Regards,
Jafar