How to write output of an octet-stream to a file while the stream is being downloaded ?
I receive the stream as a blob and then download it at the end of the stream, but how to receive the
stream and write to a file ?
(something like a FileStream that takes a stream and writes to a file)
AJAX code below
$.ajax({
    type: "post",
    url: apiURL,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(String(strCustomData)),    
    xhrFields: {
        responseType: "blob", // <-- can also be an arrayBuffer
        onprogress: function(e) { // <-- get response body here, read its stream and save in file
            // TODO
        }
    },
    cache: false,
    headers: { "RequestVerificationToken": get_CSRF_TOKEN() },
    success: function (response, textStatus, request) {
        const requestHeaders = request.getResponseHeader("Content-Disposition") || "";
        const requestContentType = request.getResponseHeader("Content-Type") || "application/zip";
        const strFileName = requestHeaders?.split(";")[1]?.split("=")[1] || "File_Not_Found.zip";
        const blob = response;
        const url = window.URL || window.webkitURL;
        const urlLink = url.createObjectURL(blob);
        // create Anchor element that holds the blob and then download it as a file
        const elementAnchor = document.createElement("a");
        elementAnchor.setAttribute("href", urlLink);
        elementAnchor.setAttribute("download", strFileName);
        // download the file
        elementAnchor.click();
       // dispose blob object and delete it from memory
       url.revokeObjectURL(urlLink);
       elementAnchor.remove();
    },
    error: function (xhr, ajaxOptions, thrownError) {
        // More validation code...
        console.error(String(xhr.statusText));
    }
});
I am looking for something like the anchor download tag <a asp-page-handler="apiURL" download>Download</a> where it receives a stream and downloads its without using a blob.
I would have used the anchor tag, but I need some Javascript code to be executed alongside it.
(Progress bar, some required custom info to display, ...etc.)
I tried using StreamSaver.js but could not make it work in the onprogress section of the AJAX request.
how to get the response.body.getReader() in onprogress event ?
how to save stream to a file without waiting for the download to complete ?
Project is using JQuery inside Razor pages with Asp.Net Core 6.