SfFileManager - How to set the root folder on the fly?
Hi there,
In all real-world scenarios we need this basic functionality:
- define an overall root folder within the API (can be hard coded, like you did it in your API demo)
- dynamically create subfolders for each user-account or project (on-the-fly, if it does not exists)
- set the sub directory as a personal root folder within FileManager component (e.g. "SetRootFolder"), so that you cannot access or view other users' documents
//Data/Files
//Data/Files/User1
//Data/Files/User2
//Data/Files/User3
<SfFileManager SetRootFolder="/User1/"> <FileManagerAjaxSettings Url="/api/FileData/FileOperations" DownloadUrl="/api/FileData/Download" GetImageUrl="/api/FileData/GetImage" UploadUrl="/api/FileData/Upload"> FileManagerAjaxSettings> SfFileManager>
So basically we must inject "SetRootFolder" in the API, somehow:
namespace FileManager.Server.Controllers { [Route("api/[controller]")] public class FileDataController : Controller { public PhysicalFileProvider operation; public string basePath; [Obsolete] public FileDataController(IHostingEnvironment hostingEnvironment) { this.basePath = hostingEnvironment.ContentRootPath; this.operation = new PhysicalFileProvider(); //SetRootFolder this.operation.RootFolder(this.basePath + "\\wwwroot\\uploads"); }
How to achive this within SyncFusion Blazor Server?
Any ideas?
Cheers,
Volker
I'm really impressed by the quality of Syncfusion's support. THANK YOU!
I'll try to solve it this way.
On the other hand, when you, being the central authority of your provided high quality components, add this property as a basic functionality and make it available on your server for downloading, all these problems would not arise.
Just a suggestion ...
Cheers,
Volker
<SfFileManager SetRootFolder="/b93f06db-1693-4060-8ad4-6e440b0ea369/"> <FileManagerAjaxSettings Url="/api/FileData/FileOperations" DownloadUrl="/api/FileData/Download" GetImageUrl="/api/FileData/GetImage" UploadUrl="/api/FileData/Upload"> FileManagerAjaxSettings> SfFileManager>
Volker
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
} |
|
public class DefaultController : Controller
{
public PhysicalFileProvider operation;
public string basePath;
string root = "wwwroot\\Files\\Uploads";
[Obsolete]
public DefaultController(IHostingEnvironment hostingEnvironment)
{
this.basePath = hostingEnvironment.ContentRootPath;
this.operation = new PhysicalFileProvider();
this.operation.RootFolder(this.basePath + "\\" + this.root);
}
} |
That was not the question.
We define a hard-coded base-folder.
Each project needs some assets like pdfs etc.
The file-manager points to this subfolder, dynamically.
(hard-coded) base-folder:
"wwwroot/Files/Uploads/"
(dynamic) root-folders:
<SfFileManager SetRootFolder="project3"> <FileManagerAjaxSettings Url="/api/FileData/FileOperations" DownloadUrl="/api/FileData/Download" GetImageUrl="/api/FileData/GetImage" UploadUrl="/api/FileData/Upload"> FileManagerAjaxSettings> SfFileManager>
Thank you so much!
|
<SfFileManager @ref="Filemanager" ShowThumbnail="false">
<FileManagerEvents OnSend="send" BeforeDownload="beforeDownload"></FileManagerEvents>
</SfFileManager>
public void send(BeforeSendEventArgs args)
{
string AjaxSettingsString = JsonConvert.SerializeObject(args.AjaxSettings);
Dictionary<string, dynamic> AjaxSettings = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(AjaxSettingsString);
string dataString = AjaxSettings["data"];
Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(dataString);
// Fetch the DropDown value.
var Rootpath = "wwwroot\\Files\\Uploads";
data.Add("FilePath", Rootpath);
string modifiedDataString = JsonConvert.SerializeObject(data);
AjaxSettings["data"] = modifiedDataString;
string returnString = JsonConvert.SerializeObject(AjaxSettings);
args.AjaxSettings = JsonConvert.DeserializeObject<object>(returnString);
} |
|
public class FileManagerDirectoryContent1
{
public string Path { get; set; }
public string Action { get; set; }
...
public string FilePath { get; set; }
}
public object FileOperations([FromBody] FileManagerDirectoryContent1 args)
{
this.operation.RootFolder(this.basePath + "\\" + args.FilePath);
if (args.Action == "delete" || args.Action == "rename")
{
if ((args.TargetPath == null) && (args.Path == ""))
{
FileManagerResponse response = new FileManagerResponse();
response.Error = new ErrorDetails { Code = "401", Message = "Restricted to modify the root folder." };
return this.operation.ToCamelCase(response);
}
}
} |
|
public void beforeDownload(BeforeDownloadEventArgs args)
{
string data = JsonConvert.SerializeObject(args.Data);
Dictionary<string, dynamic> AjaxSettings1 = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(data);
AjaxSettings1["FilePath"] = "wwwroot\\Files\\Uploads";
string returnString1 = JsonConvert.SerializeObject(AjaxSettings1);
args.Data = JsonConvert.DeserializeObject<object>(returnString1);
} |
In your example
"New Folder" works
"Rename" works
"Delete" works
"Cut and Paste" works
Cheers,
Volker
|
public void send(BeforeSendEventArgs args)
{
if (args.Action != "Upload")
{
string AjaxSettingsString = JsonConvert.SerializeObject(args.AjaxSettings);
Dictionary<string, dynamic> AjaxSettings = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(AjaxSettingsString);
string dataString = AjaxSettings["data"];
Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(dataString);
// Fetch the DropDown value.
var Rootpath = "wwwroot\\Files\\Uploads";
data.Add("FilePath", Rootpath);
string modifiedDataString = JsonConvert.SerializeObject(data);
AjaxSettings["data"] = modifiedDataString;
string returnString = JsonConvert.SerializeObject(AjaxSettings);
args.AjaxSettings = JsonConvert.DeserializeObject<object>(returnString);
}
} |
|
public void ConfigureServices(IServiceCollection services)
{
services.AddSyncfusionBlazor();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSession();
} |
|
[Route("FileOperations")]
public object FileOperations([FromBody] FileManagerDirectoryContent1 args)
{
// Store the root path in sesssion.
HttpContext.Session.SetString("RootPath", args.FilePath);
this.operation.RootFolder(this.basePath + "\\" + args.FilePath);
} |
|
[Route("Upload")]
public IActionResult Upload(string path, IList<IFormFile> uploadFiles, string action, string FilePath)
{
var rootpath = HttpContext.Session.GetString("RootPath");
this.operation.RootFolder(this.basePath + "\\" + rootpath);
}
[Route("Download")]
public IActionResult Download(string downloadInput)
{
var rootpath = HttpContext.Session.GetString("RootPath");
this.operation.RootFolder(this.basePath + "\\" + rootpath);
}
[Route("GetImage")]
public IActionResult GetImage(FileManagerDirectoryContent1 args)
{
var rootpath = HttpContext.Session.GetString("RootPath");
this.operation.RootFolder(this.basePath + "\\" + rootpath);
} |
public class myFileManagerDirectoryContent : FileManagerDirectoryContent { public string FilePath { get; set; } } [Route("api/[controller]")] public class DefaultController : Controller { ... private const string BASEFOLDER = "wwwroot\\Files\\Uploads\\"; [Route("FileOperations")] public object FileOperations([FromBody] myFileManagerDirectoryContent args) { // Store the root path in session. HttpContext.Session.SetString("RootPath", BASEFOLDER + args.FilePath); var rootpath = HttpContext.Session.GetString("RootPath"); this.operation.RootFolder(this.basePath + "\\" + rootpath);
<SfFileManager RootAliasName="Your Project"> <FileManagerEvents OnSend="@(args => Send(args,"b93f06ab-1693-4060-88d4-6e440b0ea369"))">FileManagerEvents> <FileManagerAjaxSettings Url="/api/Default/FileOperations" GetImageUrl="/api/Default/GetImage" UploadUrl="/api/Default/Upload" DownloadUrl="/api/Default/Download"> FileManagerAjaxSettings> SfFileManager> @code { public void Send(BeforeSendEventArgs args, string Rootpath) { string AjaxSettingsString = JsonConvert.SerializeObject(args.AjaxSettings); Dictionary<string, dynamic> AjaxSettings = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(AjaxSettingsString); string dataString = AjaxSettings["data"]; Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(dataString); data.Add("FilePath", Rootpath); string modifiedDataString = JsonConvert.SerializeObject(data); AjaxSettings["data"] = modifiedDataString; string returnString = JsonConvert.SerializeObject(AjaxSettings); args.AjaxSettings = JsonConvert.DeserializeObject<object>(returnString); } }
So here we go:
One last question:
Is it possible to open all subfolders within navigation pane on inital load?
One last wish:
Volker
Attachment: SetRootFolder_ab452b9f.zip
Could you please make a modification to your controller file (where you add the Blazor-App root folder option) that you sent me?
Thank you so much.
Cheers,
Volker
Attachment: FileDataController_ca246bc2.zip
|
public void send(BeforeSendEventArgs args)
{
if (args.Action != "Upload")
{
string AjaxSettingsString = JsonConvert.SerializeObject(args.AjaxSettings);
Dictionary<string, dynamic> AjaxSettings = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(AjaxSettingsString);
string dataString = AjaxSettings["data"];
Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(dataString);
// Fetch the DropDown value.
var FilePath = "filepath";
var Blobpath = "blobpath";
data.Add("BlobPath", Blobpath);
data.Add("FilePath", FilePath);
string modifiedDataString = JsonConvert.SerializeObject(data);
AjaxSettings["data"] = modifiedDataString;
string returnString = JsonConvert.SerializeObject(AjaxSettings);
args.AjaxSettings = JsonConvert.DeserializeObject<object>(returnString);
}
}
Controller:
[Obsolete]
public HomeController(IHostingEnvironment hostingEnvironment)
{
this.operation = new AzureFileProvider();
this.operation.RegisterAzure("<--accountName-->", "<--accountKey-->", "<--blobName-->");
}
[Route("AzureFileOperations")]
public object AzureFileOperations([FromBody] FileManagerDirectoryContent1 args)
{
// Store the root path in sesssion.
HttpContext.Session.SetString("FilePath", args.FilePath);
HttpContext.Session.SetString("Blobpath", args.Blobpath);
this.operation.SetBlobContainer(args.Blobpath, args.FilePath);
if (args.Path != "")
{
string startPath = args.Blobpath;
string originalPath = (args.FilePath).Replace(startPath, "");
args.Path = (originalPath + args.Path).Replace("//", "/");
args.TargetPath = (originalPath + args.TargetPath).Replace("//", "/");
}
}
// Uploads the file(s) into a specified path
[Route("AzureUpload")]
public ActionResult AzureUpload(FileManagerDirectoryContent args)
{
var filepath = HttpContext.Session.GetString("FilePath");
var blobpath = HttpContext.Session.GetString("Blobpath");
this.operation.SetBlobContainer(blobpath, filepath);
if (args.Path != "")
{
string startPath = blobpath;
string originalPath = (filepath).Replace(startPath, "");
args.Path = (originalPath + args.Path).Replace("//", "/");
}
return Json("");
} |
Hello,
I would like to ask, there are a built in way now in new versions to set root folder from View ( SetRootFolder or similar?)
If not, there are a solution to add custom parameter to FileManagerDirectoryContent args from View trough TagHelper?
Thank you!
Hi SZL,
Greetings from Syncfusion support
We would like to inform you that we have provided support to send JWT tokens in Read operation from client to server. By using the JWT token, you can send dynamic root folder names from the client-side to the server-side. Based on the retrieved server-side root folder name, you can set dynamic root folders in the FileManager component.
Check the shared sample.
Sample: attached as zip file.
The above sample demonstrates how to pass the root folder from the client-side to the server-side for FileManager read/upload operation. Based on the server-side value, we have changed the FileManager root folder to client1 and client2 dynamically.
Also, we have shared a GitHub sample that contains details related to sending JWT tokens from the client-side to the server-side in all FileManager operations and how to set a dynamic root folder in the FileManager component for your reference.
GitHub sample: https://github.com/SyncfusionExamples/blazor-filemanager-pass-jwt-token
Regards,
Vishwanathan
Attachment: BlazorFileManager_a1d0b0d5.zip
- 17 Replies
- 5 Participants
-
VO Volker
- May 19, 2020 05:32 PM UTC
- Dec 6, 2024 12:36 PM UTC