将二进制格式的文件发布到Angular 7的POST请求URL中

时间:2018-12-10 06:46:39

标签: angular curl http-post

在这里,我们有一个curl命令,我们需要在angular应用程序中执行相同的命令。

curl -X POST <url> --data-binary @<filenname>.dcm

这是我们所做的: 首先,我们上传了文件: HTML

<form>
<input type="file" class="inputFile" (change)="selectFileM($event)">
<button (click)="submit()">submit</button>
</form>

.ts文件

selectFileM(event) {
    let snap;
    this.selectedFiles = event.target.files;
    console.log(this.selectedFiles);
    const file = this.selectedFiles.item(0);
    console.log(file);
    this.currentFileUpload = new Fileup(file);
    console.log(this.currentFileUpload);
    this.userService.upload_orthanc(this.currentFileUpload).subscribe(data => {
      console.log(data);
    });

服务:

    upload_orthanc(model) {
        let fd: FormData = new FormData();
        fd.append('file', model, model.name);
        var reader  = new FileReader();
        reader.readAsBinaryString(model);
        const a = reader.result;
        return this.http.post('https://trdorthanc.therightdoctors.com/instances', a, this.jwt());
      }
private jwt() {
      let headers = new Headers({'content-type': 'application/dicom' });
      return new RequestOptions({headers: headers});
  }

我们收到一个错误: 错误TypeError:无法在'FileReader'上执行'readAsBinaryString':参数1不是'Blob'类型。

检查屏幕截图: enter image description here

请提出一些解决方案

1 个答案:

答案 0 :(得分:1)

readAsBinaryString(blob)。在您的情况下,您没有将文件更改为BLOB。

const modelData= new Blob([model], { type: "application/image" });
.
.
.
reader.readAsBinaryString(modelData);
相关问题