Skip to content

Commit 677ab0c

Browse files
Integrated latest changes at 03-18-2025 4:30:43 AM
1 parent 4fb0464 commit 677ab0c

File tree

2 files changed

+26
-85
lines changed

2 files changed

+26
-85
lines changed

ej2-vue/file-manager/how-to/pass-custom-value-to-server.md

Lines changed: 24 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ domainurl: ##DomainURL##
1010

1111
# Pass custom value to server in Vue File Manager component
1212

13-
The Syncfusion Vue File Manager component allows you to pass custom values from the client to the server for various operations. This guide demonstrates how to implement this functionality for **Upload**, **Download**, and **GetImage** operations using the [`beforeSend`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforesend), [`beforeDownload`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforedownload), and [`beforeImageLoad`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforeimageload) events respectively.
13+
The Syncfusion Vue File Manager component allows seamless back-end server interaction by passing custom values. This enhances the functionality and security of file operations, particularly helpful for tasks like authentication, logging, or user role-based processing. In multi-user systems, it ensures file access permissions and actions are user-specific and secure. You can manage user-specific operations such as **Read**, **Delete**, **Rename**, **Create**, **Move**, **Copy**, **Details**, **Search**, **Upload**, **Download**, and **GetImage** using custom headers or query parameters. This guide demonstrates implementing these features using the [`beforeSend`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforesend), [`beforeDownload`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforedownload) and [`beforeImageLoad`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforeimageload) events. Let's explore how to achieve this in [`Physical file system provider`](https://github.com/SyncfusionExamples/ej2-aspcore-file-provider).
1414

15-
## Upload Operation
15+
## 1. Setting up the File Manager and provider
1616

17-
To pass custom values during the upload operation, utilize the [`beforeSend`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforesend) event of the File Manager component.
17+
To create a basic File Manager component, start by following the easy steps in the [`Getting Started`](https://ej2.syncfusion.com/vue/documentation/file-manager/getting-started) guide. This will allow you to manage files and folders on your system, whether they are stored physically or in the cloud.
1818

19-
### Client-side Implementation
19+
For connecting the File Manager to a physical file system, check out the [`Physical file provider`](https://ej2.syncfusion.com/vue/documentation/file-manager/file-system-provider#physical-file-system-provider) section. This part of the documentation will help you configure it correctly.
20+
21+
## 2. Handling File Operations
22+
23+
After setting the File Manager component with the physical file system provider, you can handle file operations by passing custom values to the server. To pass custom values during the **Read**, **Delete**, **Rename**, **Create**, **Move**, **Copy**, **Details**, **Search** and **Upload** operations, utilize the [`beforeSend`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforesend) event of the File Manager component. This event allows you to modify the request before it is sent to the server. You can add custom headers to the request to pass additional information to the server.
24+
25+
The `onBeforeSend` function is designed to enhance security by adding an authorization header to every outgoing AJAX request. Before a request is sent, this function is called, and it attaches the **Authorization** header with the value **User1** to the request. This ensures that the server can verify the request's authenticity and handle it accordingly.
2026

2127
```ts
2228

@@ -59,7 +65,7 @@ export default {
5965

6066
```
6167

62-
### Server-side Implementation (C#)
68+
In server-side, `FileOperations` and `Upload` methods access the **Authorization** header from the incoming HTTP request and perform the necessary operations.
6369

6470
```typescript
6571

@@ -70,8 +76,6 @@ public class FileManagerController : Controller
7076
public object FileOperations([FromBody] FileManagerDirectoryContent args)
7177
{
7278
var header = HttpContext.Request.Headers["Authorization"];
73-
this.root = (header == "User1") ? "wwwroot\\FileBrowser" : "wwwroot\\Files";
74-
this.operation.RootFolder(this.basePath + "\\" + this.root);
7579
...
7680
}
7781

@@ -87,11 +91,10 @@ public class FileManagerController : Controller
8791

8892

8993
```
90-
## Download Operation
91-
For the download operation, use the [`beforeDownload`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforedownload) event to send custom values to the server.
9294

93-
### Client-side Implementation
95+
## 3. Handling Download Operation
9496

97+
For the **download** operation, use the [`beforeDownload`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforedownload) event, setting [`useFormPost`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/beforeDownloadEventArgs/#useformpost) as false to use a fetch request to send the custom header in **beforeSend** event. Here an **Authorization** header is appended to fetch request headers with the value **User1**.
9598

9699
```ts
97100

@@ -120,55 +123,12 @@ export default {
120123
},
121124
methods: {
122125
onBeforeDownload: function (args) {
123-
args.cancel = true;
124-
if(args.data){
125-
var obj= {
126-
action: 'download',
127-
path: args.data.path,
128-
names:args.data.names,
129-
data: args.data.data,
126+
args.useFormPost = false;
127+
if (args.ajaxSettings) {
128+
(args.ajaxSettings as any).beforeSend = function (args: any) {
129+
args.fetchRequest.headers.append('Authorization', 'User1');
130130
};
131131
}
132-
var xhr = new XMLHttpRequest();
133-
xhr.open(
134-
'POST',
135-
'http://localhost:{port}/api/FileManager/Download',
136-
true);
137-
xhr.responseType = 'blob';
138-
xhr.onload = function () {
139-
if (this.status === 200) {
140-
var name = '';
141-
// Getting file name from content-disposition header
142-
let header = xhr.getResponseHeader('Content-Disposition');
143-
if (header && header.indexOf('attachment') !== -1) {
144-
var regex = /name[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
145-
var matches = regex.exec(header);
146-
if (matches != null && matches[1])
147-
name = matches[1].replace(/['"]/g, '');
148-
}
149-
//saving the file locally using anchor tag
150-
var blob = new Blob([this.response], {
151-
type: xhr.getResponseHeader('Content-Type'),
152-
});
153-
var anchorUrl = window.URL.createObjectURL(blob);
154-
if (name) {
155-
var anchor = document.createElement('a');
156-
anchor.href = anchorUrl;
157-
anchor.download = name;
158-
anchor.click();
159-
} else {
160-
window.location = anchorUrl;
161-
}
162-
setTimeout(function () {
163-
URL.revokeObjectURL(anchorUrl);
164-
}, 100);
165-
}
166-
};
167-
var fdata = new FormData();
168-
fdata.append('downloadInput', JSON.stringify(obj));
169-
//Custom Header Added to XHR
170-
xhr.setRequestHeader('Authorization', 'User1');
171-
xhr.send(fdata);
172132
},
173133
}
174134
provide: {
@@ -178,24 +138,21 @@ export default {
178138

179139
```
180140

181-
### Server-side Implementation (C#)
141+
In server-side, `Download` method access the **Authorization** header from the incoming HTTP request and perform the necessary operations.
182142

183143
```typescript
184144
[Route("Download")]
185-
public IActionResult Download(string downloadInput)
145+
public object Download([FromBody] FileManagerDirectoryContent args)
186146
{
187-
var header = Request.Headers["Authorization"].ToString();
188-
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
147+
var header = HttpContext.Request.Headers["Authorization"];
189148
...
190149
}
191150

192151
```
193152

194-
## GetImage Operation
195-
196-
For the GetImage operation, use the [`beforeImageLoad`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforeimageload) event to pass custom values as query parameters.
153+
## 4. For GetImage Operation
197154

198-
### Client-side Implementation
155+
For the **GetImage** operation, use the [`beforeImageLoad`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/#beforeimageload) event to pass custom value. Since the **GetImage** operation doesn't support custom headers in HTTP requests, pass the custom values along with [`imageUrl`](https://ej2.syncfusion.com/vue/documentation/api/file-manager/beforeImageLoadEventArgs/#imageurl) using query parameters instead.
199156

200157
```ts
201158

@@ -234,7 +191,7 @@ export default {
234191

235192
```
236193

237-
### Server-side Implementation (C#)
194+
In server-side, you can able to get the custom query parameter value using **Authorization** in `GetImage` method. To get the custom query parameter value, extend the `FileManagerDirectoryContent` class and add the custom property **Authorization**.
238195

239196
```typescript
240197

@@ -261,22 +218,6 @@ public class FileManagerAccessController : Controller
261218

262219
```
263220

264-
## Implementing custom value transfer in Syncfusion File Manager with server-side integration.
265-
266-
The below file system provider allows the users to access and manage the file system which includes the server side code for custom values passing from client. To get started, clone the [provider](https://github.com/SyncfusionExamples/How-to-pass-custom-values-from-client-to-server-in-filemanager) using the following command.
267-
268-
```typescript
269-
270-
git clone https://github.com/SyncfusionExamples/How-to-pass-custom-values-from-client-to-server-in-filemanager How-to-pass-custom-values-from-client-to-server-in-filemanager
271-
272-
```
273-
274-
After cloning, just open the project in Visual Studio and restore the NuGet packages. The root directory of the provided file system in the File Manager controller is **Files** for **User1**. Now, just build and run the project.
221+
> **Note:** View the complete [Github sample](https://github.com/SyncfusionExamples/How-to-pass-custom-values-from-client-to-server-in-filemanager) which includes all the above event along with service code for passing custom values to server.
275222
276-
Run the sample which includes all the above event for passing custom values to server: [How-to-pass-custom-values-from-client-to-server-in-File-Manager](https://stackblitz.com/edit/custom-values-from-client-to-server-in-vue-filemanager?file=src%2FApp.vue)
277-
278-
```ts
279-
url: "http://localhost:{port}/api/FileManager/FileOperations", // Replace {port} with host wherever used.
280-
```
281223

282-
The project will be hosted and map the local host in the **ajaxSettings** property of the File Manager component.

ej2-vue/file-manager/upload.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ If you want to allow only image files like .jpg and .png, you would set the prop
124124

125125
The File Manager component provides support for external drag-and-drop functionality for uploading files by dragging it from local file system to File Manager.
126126

127-
Setting [allowDragAndDrop](https://ej2.syncfusion.com/vue/documentation/api/file-manager#allowdraganddrop) property to false will not prevent the file upload operation through external drag and drop. It will only prevent drag and drop action within the File Manager component
128-
129127
To completely prevent the external drag-and-drop upload functionality (i.e., disallowing users from dragging and dropping files from outside into the File Manager), you can set the [dropArea](https://ej2.syncfusion.com/vue/documentation/api/uploader#droparea) property to null. This can be done by accessing the File Manager instance via its class methods.
130128

129+
>**Note:** Setting [allowDragAndDrop](https://ej2.syncfusion.com/vue/documentation/api/file-manager#allowdraganddrop) property to false will not prevent the file upload operation through external drag and drop. It will only prevent drag and drop action within the File Manager component.
130+
131131
The following example demonstrates how to prevent the external drag and drop upload actions for all types of files in the File Manager component.
132132

133133
{% tabs %}

0 commit comments

Comments
 (0)