How to Add an Alert in the FileStore Method in Telerik File Explorer?

1 Answer 14 Views
FileExplorer
Kavindu
Top achievements
Rank 1
Kavindu asked on 27 Mar 2025, 04:33 AM
I'm trying to add a alert inside the FileStore method in Telerik's File Explorer, but it's not working as expected. Here's my implementation:

public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)
{          
    string physicalPath = this.GetPhysicalFromVirtualPath(path);
    if (physicalPath == null)
        return string.Empty;

    Directory.CreateDirectory(physicalPath);

    // sanitize the file name
    name = (name.Replace(" ", "_")).Replace("'", string.Empty);
    physicalPath = PathHelper.AddEndingSlash(physicalPath, '\\') + name;
    
    string fileName = Path.GetFileName(physicalPath);
    string folderPath = Path.GetDirectoryName(physicalPath);
      
    FileStorageManager fileStorageManager = new FileStorageManager();
    FileStorageInformation fileInfo = fileStorageManager.GetFileByFolderAndFileName(_applicationID, folderPath, fileName);

    string fileFolderRelativeToRoot = path;
    string rootToThisContext = path;
    string message = null;

    if (File.Exists(physicalPath))
    {
        bool isVersioningDisabledOnDestination = false;
        UploadingHandler uploadingHandler = new UploadingHandler();
        uploadingHandler.ExecuteAction(fileInfo, fileName, fileFolderRelativeToRoot, rootToThisContext, _applicationID, _formID, _userID, ref isVersioningDisabledOnDestination);

        if (isVersioningDisabledOnDestination)
        {
            message = string.Format("Can not upload - versioning not enabled on target folder");
        }
    }

    if(message == null)
    {
        file.SaveAs(physicalPath);

        return PathHelper.AddEndingSlash(path, '/') + name;
    }
    else
    {
        return message;
    }         
}

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 28 Mar 2025, 10:18 AM

Hi Kavindu,

You can register a JavaScript alert directly inside the FileBrowserContentProvider.StoreFile method. Here is an example using as a base the \App_Code\Editor\DBContentProvider.cs file of the Custom File Content Provider demo:

		public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)
		{
			long fileLength = file.InputStream.Length;
			byte[] content = new byte[fileLength];
			file.InputStream.Read(content, 0, (int)fileLength);

			string error = dataServer.StoreFile(name, path, file.ContentType, content);

            string script = string.Format("alert('{0}');", "message text");// The JavaScript code
            Page page = HttpContext.Current.Handler as Page;
            ScriptManager.RegisterStartupScript(page, GetType(), "Error", script, true);// Register the code

                       return String.IsNullOrEmpty(error) ? String.Format("{0}{1}{2}", path, PathSeparator, name) : String.Empty;
		}

You can find more information in the Executing JavaScript Code from Server-side in Web Forms and ASP.NET AJAX article.

For your scenario, you can try the following:

        public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)
        {
            string physicalPath = this.GetPhysicalFromVirtualPath(path);
            if (physicalPath == null)
                return string.Empty;

            Directory.CreateDirectory(physicalPath);

            // sanitize the file name
            name = (name.Replace(" ", "_")).Replace("'", string.Empty);
            physicalPath = PathHelper.AddEndingSlash(physicalPath, '\\') + name;

            string fileName = Path.GetFileName(physicalPath);
            string folderPath = Path.GetDirectoryName(physicalPath);

            FileStorageManager fileStorageManager = new FileStorageManager();
            FileStorageInformation fileInfo = fileStorageManager.GetFileByFolderAndFileName(_applicationID, folderPath, fileName);

            string fileFolderRelativeToRoot = path;
            string rootToThisContext = path;
            string message = null;

            if (File.Exists(physicalPath))
            {
                bool isVersioningDisabledOnDestination = false;
                UploadingHandler uploadingHandler = new UploadingHandler();
                uploadingHandler.ExecuteAction(fileInfo, fileName, fileFolderRelativeToRoot, rootToThisContext, _applicationID, _formID, _userID, ref isVersioningDisabledOnDestination);

                if (isVersioningDisabledOnDestination)
                {
                    message = "Cannot upload - versioning not enabled on target folder";

                    // Inject JS alert
                    Page page = HttpContext.Current.Handler as Page;
                    if (page != null)
                    {
                        ScriptManager.RegisterStartupScript(page, GetType(), "VersioningError", $"alert('{message}');", true);
                    }
                }
            }

            if (message == null)
            {
                file.SaveAs(physicalPath);

                return PathHelper.AddEndingSlash(path, '/') + name;
            }
            else
            {
                return message;
            }
        }

Regards,
Rumen
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Tags
FileExplorer
Asked by
Kavindu
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or