angular - 如何在Angular HTTP中获得临时响应-ArrayBuffer

标签 angular electron httpresponse angular-http arraybuffer

我内部有一个Electron应用程序,正在加载Angular应用程序。我正在通过API调用下载字节数组(即ArrayBuffer),并将这些数据传递给我通过electron.remote.require('./file-service')连接以在本地创建文件的方法文件系统。

如果用户更改了路线,将弹出一个窗口提示您确认导航。如果用户单击确定,并且http请求介于两者之间,则我需要存储接收到的字节。

示例语言代码:

declare var electron: any;
const { createDataFile } = electron.remote.require('./file-service')

const payLoad = new FormData();
const httpOptions =  {
      headers: new HttpHeaders(),
      reportProgress: true,
    };

const req = new HttpRequest('GET', 'http://localhost:8080/getData', payLoad, {...httpOptions, responseType: 'arraybuffer'});
this.http.request<ArrayBuffer>(req).subscribe((event: HttpEvent<ArrayBuffer>) => {

    switch (event.type) {
        case HttpEventType.DownloadProgress:
            // This method will manipulate and show the progress bar in the UI
            this.updateProgress(event.loaded);
        break;
        case HttpEventType.Response:
            createDataFile(event.body)
        break;
    }

});

我正在尝试保存数据,如果arraybuffer的大小为25MB,而我却收到了12MB,并且我正试图浏览,那一刻,我需要存储该12MB。
请帮助我如何获得临时答复。

最佳答案

您提到可以控制用Java编写的后端代码。我不确定是否可以将responseType设置为文本,然后将编码的字节数组作为字符串发送。但是如果可以的话,您可以从接收到的事件中获取partialText中的临时数据以对其进行解码和存储。

     interface HttpDownloadProgressEvent extends HttpProgressEvent {
      type: HttpEventType.DownloadProgress
      partialText?: string  //The partial response body as downloaded so far.Only present if the responseType was text.

      // inherited from common/http/HttpProgressEvent
      type: HttpEventType.DownloadProgress | HttpEventType.UploadProgress
      loaded: number
      total?: number
    }

    switch (event.type) {
        case HttpEventType.DownloadProgress:
            // This method will manipulate and show the progress bar in the UI
            this.updateProgress(event.loaded);
//////////////////////////////////////////////////////////////////////////////
            store event.partialText; 
/////////////////////////////////////////////////////////////////
        break;
        case HttpEventType.Response:
            createDataFile(event.body)
        break;
    }

不要忘记requestProgress
const req = new HttpRequest('POST', 'upload/file', file, {
  requestProgress: true
});

关于angular - 如何在Angular HTTP中获得临时响应-ArrayBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359813/

相关文章:

javascript - Angular 7 | HostListener 模糊条件停止所有其他事件回调

javascript - 从8到9的 Angular 更新给测试框架带来了问题

javascript - 未捕获的类型错误 : Cannot read property 'fn' of undefined, Electron 2, Angular 6?

rest - ContainerRequestFilter ContainerResponseFilter 不会被调用

c# - HTTP header 中内容处置的替代方案 (c#)

node.js - 如何从node.js后端获取客户端的错误对象?

angular - 'authGuard' 中的 canActivate 无法读取 AuthServer 的 ' observable current user '

javascript - 将 boolean 值传递给另一个 Angular 组件

json - 在dart中将Map毫秒毫秒SinceEpoch转换为DateTime

javascript - Electron 桌面应用程序 - 通过传递参数加载 url - javascript