简体   繁体   English

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

[英]How to get interim response in Angular HTTP - ArrayBuffer

I'm having a Electron application internally I'm loading the Angular application. 我内部有一个Electron应用程序,正在加载Angular应用程序。 I'm downloading a byte array (ie, ArrayBuffer) through an API call and passing those data to a method which I'm connecting through electron.remote.require('./file-service') to create a file in a local file system. 我正在通过API调用下载字节数组(即ArrayBuffer),并将这些数据传递给我通过electron.remote.require('./ file-service')连接以在本地创建文件的方法文件系统。

If the User changes the route, a popup window will prompt to get the confirmation of navigation. 如果用户更改了路线,将弹出一个窗口提示您确认导航。 If the User clicks ok and the http request is in between, I need to store the received bytes. 如果用户单击确定,并且http请求介于两者之间,则需要存储接收到的字节。

Sample Angular Code: 示例角度代码:

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;
    }

});

I'm trying to save data, If the arraybuffer size is 25MB and I received 12MB and I'm trying to navigate away, in that moment I need to store that 12MB. 我正在尝试保存数据,如果arraybuffer的大小为25MB,而我却收到了12MB,并且我正试图离开,那一刻,我需要存储12MB。 Kindly assist me how to get the interim response. 请帮助我如何获得临时答复。

You mentioned you can control the back end code written in java. 您提到可以控制用Java编写的后端代码。 I am not sure if you can make responseType a text and send encoded byte array as a string. 我不确定是否可以将responseType设置为文本并将编码的字节数组作为字符串发送。 But if you can then you can get interim data in partialText from the event you receive to decode and store it. 但是如果可以的话,您可以从接收到的事件中获取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;
    }

Don't forget requestProgress 不要忘记requestProgress

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM