ej2-angular-spreadsheet Add Authorization header on chunked open requests (all chunks)

Hi Syncfusion Support Team,

I’m using ej2-angular-spreadsheet with chunked response to open large Excel files.
Our API requires a Bearer token on every request. I can add the header on the first open request via beforeOpen, but subsequent chunk requests don’t include the header and the server returns 401.

<ejs-spreadsheet

  [openUrl]="openUrl"

  [openSettings]="{ chunkSize: 1_000_000, retryCount: 3, retryAfterDelay: 500 }"

  (beforeOpen)="beforeOpen($event)">

</ejs-spreadsheet>


beforeOpen(args: any) {

  args.requestData = {

    ...(args.requestData || {}),

    headers: { Authorization: `Bearer ${this.tokenService.getToken()}` }

  };

}

[HttpPost("Open")]

public IActionResult Open(IFormCollection openRequest) {

    var open = new OpenRequest();

    if (openRequest.Files.Count > 0) open.File = openRequest.Files[0];

    if (openRequest.TryGetValue("chunkPayload", out var chunkPayload))

        open.ChunkPayload = chunkPayload; // for chunked response

    var result = Workbook.Open(open, 150);

    return Content(result);

}


Attachment: 20250912_152912_f052fcc2.png

3 Replies

BP Babu Periyasamy Syncfusion Team September 16, 2025 11:19 AM UTC

Hi Aphisit Seethinwan,


We have reviewed your request based on the details you provided. We'd like to clarify that the Spreadsheet's Chunk Response Processing feature is designed to efficiently handle large Excel files containing extensive data and features. It works by dividing the server response into smaller segments, known as chunks, which are sent to the client in parallel. This approach optimizes performance and helps prevent memory issues or connection interruptions during data transmission. Once all chunks are received, the client combines them to load the Excel data smoothly into the Spreadsheet.

For more information, please refer to the below documentation: 


https://help.syncfusion.com/document-processing/excel/spreadsheet/angular/open-save#chunk-response-processing


When an open process is initiated in the Spreadsheet, the beforeOpen event is triggered once, before sending the initial request to the server. If a chunkSize is defined in the openSettings, the Spreadsheet handles the subsequent chunk requests internally, without triggering the beforeOpen event again. Therefore, the event is not triggered multiple times for each chunk by default.


However, we have already confirmed this area as an improvement where the beforeOpen event will be triggered for every chunk request, allowing users to add headers dynamically. This enhancement has been logged as a feature request and will be included in one of our upcoming releases. You can communicate and track the status of the feature using the below link from our feedback portal.


Feedback link: https://www.syncfusion.com/feedback/69905/provide-support-to-trigger-beforeopen-event-for-each-chunk-request-during-chunk


At the planning stage for every release cycle, we review all the open features once again and finalize features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. Once we have anything definite to share about these features implementation, we will move the feedback to scheduled status with the tentative release timeline.


In the meantime, we have prepared a workaround solution that allows you to add headers for each chunk request by patching window.fetch. This approach intercepts the component’s internal chunk requests and injects the required headers. For your convenience, we’ve prepared a sample demonstrating this configuration, where headers are properly added to each chunk request. Below is the relevant code snippet and sample output for your reference.


export class AppComponent {

    constructor() {

        this.patchFetch();

    }

    @ViewChild('default')

    public spreadsheetObj: SpreadsheetComponent;

    public data: Object[] = getDefaultData();

    public token: string;

    public openSettings: OpenSettingsModel = {

        chunkSize: 2000,

        retryCount: 3,

        retryAfterDelay: 500

      }

    public openUrl = 'https://localhost:44341/api/spreadsheet/open';

    public saveUrl = 'https://localhost:44341/api/spreadsheet/save';

    created() {

        …..

    }

    beforeOpenHandler(args) {

        args.requestData = {

            ...(args.requestData || {}),

            headers: { Authorization: `Bearer Token` }

          };

          this.token = 'Bearer Token';

    }

    

    patchFetch() {

        const originalFetch = window.fetch;

 

        window.fetch = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {

            if (this.token) {

                const newInit: RequestInit = {

                    ...init,

                    headers: {

                        ...(init?.headers || {}),

                        'Authorization': this.token,

                    },

                };

 

                return originalFetch(input, newInit);

            }

        };

    }

 

}

 

 


Sample link: https://stackblitz.com/edit/angular-5eao6bd8-ilizxgca?file=src%2Fapp.component.ts


Local service (API): Please refer to the attachment.


Output:

A white background with black and orange text

AI-generated content may be incorrect.


Please review the above shared details and get back to us if you have any further questions or concerns.


Regards,

Babu.


Attachment: WebAPI_(2)_4443cfe9.zip


YR Young Richardson September 24, 2025 04:07 AM UTC

I think beforeOpen only applies to the first request, not the chunked ones. For chunks you need to use the beforeSend event of the Spreadsheet, which lets you attach headers to every request. Try adding your Bearer token there so all chunks include it.



BP Babu Periyasamy Syncfusion Team September 24, 2025 09:28 AM UTC

Hi Young Richardson,


Thank you for your update.


We’ve reviewed the shared details and would like to clarify that there is no event named beforeSend in the Syncfusion Spreadsheet component.


As mentioned in our previous update, the beforeOpen
event is triggered during the initial request. As a workaround, we’ve added headers to chunked requests by patching window.fetch. Additionally, we’ve already logged an improvement request to trigger the beforeOpen event for chunked requests as well, as noted earlier.


However, if you’ve implemented header injection for chunked requests using a different approach, we’d appreciate it if you could share more details. This will help us further validate and potentially enhance the solution.


Please feel free to reach out if you have any further questions or concerns.


Regards,

Babu.


Loader.
Up arrow icon