Hello,
Imagine a client program that has declared HttpClient (often scoped) that you would wire to FileManager in order to be able to pass Headers before invoking FileManager, actually it seems impossible to get HttpClient from a distant injected service because I suspect FileManager to ignore default HttpClient.
Is it possible to add a new parameter to FileManager for using an HttpClient please?
@inject HttpClient Client
@using Syncfusion.Blazor.FileManager
@inject IJSRuntime jsRuntime
<div class="control-section">
<SfFileManager ID="file-manager" TValue="FileManagerDirectoryContent" View="ViewType.Details">
<FileManagerAjaxSettings Url="/api/SampleData/FileOperations"
UploadUrl="/api/SampleData/Upload"
DownloadUrl="/api/SampleData/Download"
GetImageUrl="/api/SampleData/GetImage">
</FileManagerAjaxSettings>
</SfFileManager>
</div>
@code {
protected override void OnInitialized()
{
Client.BaseAddress ??= new Uri("http://localhost:61811");
Client.DefaultRequestHeaders.Add("DestSubPath", "E5000");
}
}
Thanks
|
index.razor
<SfFileManager TValue="FileManagerDirectoryContent"> . . .
<FileManagerEvents TValue="FileManagerDirectoryContent" OnSend="onsend"></FileManagerEvents> </SfFileManager>
@code{
void onsend(BeforeSendEventArgs args)
{
args.HttpClientInstance.DefaultRequestHeaders.Add("Authorization", "user");
}
}
-------------------------------------------------- FileManagerController.cs public object FileOperations([FromBody] FileManagerDirectoryContent args) {
//to sent data at server side
var val = HttpContext.Request.Headers["Authorization"];
}
|
Hi Sowmiya ,
Thanks for your answear. The solution you've provided works well for UPLOAD and for FILEOPERATIONS using OnSend event, I can send HttpContext.Request.Headers with success and I can retrieve from these 2 operations only.
Please, can you advise me how to do same with DOWNLOAD and GETIMAGE, because it seems not to work, when I try to get HttpContext.Request.Headers from these, I don't get any data.
Is there any event like OnSend does for these 2 last operations please?
Thanks
Regards
|
public async Task beforeDownload(BeforeDownloadEventArgs<FileManagerDirectoryContent> args)
{
args.Cancel = true;
DirectoryContent[] data = new DirectoryContent[]{ new DirectoryContent()
{
Name = args.Data.DownloadFileDetails[0].Name, // name of the file
IsFile = args.Data.DownloadFileDetails[0].IsFile, // indicates whether file or folder
FilterPath = args.Data.DownloadFileDetails[0].FilterPath, // path of the file/folder from root directory
HasChild = args.Data.DownloadFileDetails[0].HasChild, // if folder has child folder set as true else false
Type = args.Data.DownloadFileDetails[0].Type // empty string for folder and file type like .png for files
} };
DownloadUrl = https://localhost:44355/api/FileManager/Download;
DirectoryContent downloadData = new DirectoryContent()
{
Data = data,
Path = args.Data.Path,// path in which the file is located (make ensure to add the path from root directory excluding the root directory name)
Names = args.Data.Names// names of the files to be downloaded in the specified path
};
await jsRuntime.InvokeAsync<object>("saveFile", downloadData, DownloadUrl);
} |
|
<script>
window.saveFile = (data, downloadUrl) => {
//creating the data to call download web API method
var i = {
action: "download",
path: data.path,
names: data.names,
data: data.data,
customvalue: "TestFolder",
}
…
//appeding the dynamically created form to the document and perform form sumbit to perform download operation
a.appendChild(s),
document.body.appendChild(a),
document.forms.namedItem("downloadForm").submit(),
document.body.removeChild(a)
}
</script> |
|
public class FileManagerDirectoryContentExtend : FileManagerDirectoryContent
{
public string customvalue { get; set; }
}
...
[Route("Download")]
public IActionResult Download(string downloadInput)
{
FileManagerDirectoryContentExtend args = JsonConvert.DeserializeObject<FileManagerDirectoryContentExtend>(downloadInput);
var val = args.customvalue; |
Hi Indhumathy,
Thanks for your reply.
I still maintain my original question because HttpClient contains all about connected client provides like his claims and more..
That is to say that actually the Filemanager's controller is quite stressing because in any way I am unable to retrieve the HttpClient's claims, so impossible to control/block at controller's side the access of the controller, that I consider as a real security issue in a production environment, plus unable to get any extra infos that HttpClient contains
Regards,
Hi Indhumathy,
sorry if my request seems fuzzy but I can take a comparison to make you understand the very problem I am facing.
At 1st, I have an ASPNET 5 Core server that holds all API Endpoints with a generic CRUD that allows me to use WebAssembly Blazor apps where a page is using a DataGrid in CustomDataAdaptor mode
AdaptorInstance="typeof(IWasmDataAdaptor<TEntity>)"
Behind this interface that holds DatagridCrud + holds an injected service...
// Add ProxyAdaptor (based on generic open type)
builder.Services.AddScoped(typeof(IWasmProxyAdaptor<>), typeof(WasmProxyAdaptor<>));
So, the class is:
public WasmProxyAdaptor(HttpClient clientHttp)
As you can see as I've injected this class to Startup, I can de facto use the injected HppClient from my Wasm App.
What's inside my HttpClient?
Everything about once I'm logged and authorized, I can use my Claims(), so I can use the HttpClient's instance to talk with my ASPNET Core server APIs!
I realy found the way that Datagrid is implemented because it allows us to use HttpClient.
Now to the FileManager... I've seen that internally we can do this:
private void OnSend(BeforeSendEventArgs args)
{
if (!args.HttpClientInstance.DefaultRequestHeaders.Any(x => x.Key.Equals("EnvId", StringComparison.OrdinalIgnoreCase)))
{
args.HttpClientInstance.DefaultRequestHeaders.Add("EnvId", EnvId + "/");
}
}
Nice at first sight... but in reality I am able to intercept extra parameters via IHttpContextAccessor
public FileController(IConfiguration configuration, IHttpContextAccessor accessor)
{
_accessor = accessor;
operation.RootFolder($"{basePath}{_accessor.HttpContext.Request.Headers["EnvId"]}");
}
Ok, nice, but... ? only that!
I can't intercept HttpClient because if i try to get 'injected' HttpClient => it is empty, no claims, no headers, well nothing, so I can't really use the component the easy way and really straightforward Datagrid component.
IMHO, it would be nice to be able to 'wire' the internal FileManager's so called HttpClient to an injected HttpClient, for Wasm client apps that are connected and authorized, then we can easily manipulate extra headers, but also get claims and manage correctly the whole machinery.
Hope my explanations enlights
Regards
|
[index.razor]
<SfFileManager TValue="FileManagerDirectoryContent"> . . .
<FileManagerEvents TValue="FileManagerDirectoryContent" OnSend="onsend"></FileManagerEvents> </SfFileManager>
@code{
void onsend(BeforeSendEventArgs args)
{
args.HttpClientInstance.DefaultRequestHeaders.Add("Authorization", "user");
}
}
-------------------------------------------------- [FileManagerController.cs] public object FileOperations([FromBody] FileManagerDirectoryContent args) {
//You can retrieve the header value at controller side.
var val = HttpContext.Request.Headers["Authorization"];
} |
Hi Indhumathy ,
This is exactly what I've done BUT BUT BUT I'm sorry to tell you that OnSend will ONLY work for getting the files initially, but NOT for other operations (Download...) since the instance is not trappable because 'internally' managed by the component... relly annoy
So, the 'onboarded' args.HttpClientInstance is not sufficient, because I ALWAYS need user's Claims each time the Controller is reached, so your solution is a half solution, not acceptable for managing/challenging Claims() at every stage from the controller.
So my request remains same about HttpClient 'globally', because when you are developping in WebAssembly, the only 'controllers' available are distant, the examples given in the official doc aere optimistic and relies upon a Blazor Server app, but are half usable for a Wasm project.
Regards
Hi Keerthana,
Thanks, really needed.
Regards
Hi,
I am facing the same issue that Gerome describes in this thread. Was there a solution provided that can be shared here?
Thank you,
Mike
Hi Mike,
As per current implementation of
File Manager component, the HttpClient instance is only sent in Read and Upload
operation since we have handled only those file operations using Http request.
You can use the OnSend event
to set HttpClientInstance for both Read and Upload
operation.
The Download operation of File Manager is handled by using form submit and GetImage operation is handled by using query parameter. We doesn’t handle it by using Http request, so it is not possible to pass the HttpClientInstance here. However, you can send custom values from client to server for Download operation. You can prevent our default download operation by setting args.Cancel as true in BeforeDownload event. Then you can trigger the customized download operation using an interop call where you can pass custom values to server side. Please check the below update.
http://www.syncfusion.com/forums/169552/how-to-use-httpclient-for-file-manager?reply=SoZ8Dd
This is the current behavior of File Manager component. Please check the shared details and get back to us if you need any further assistance.
Regards,
Indhumathy L