therightdoctors
therightdoctors

Reputation: 548

posting a file in binary format to the POST request URL in angular 7

Here, we have a curl command and we need to execute the same inside an angular application.

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

Here is what we have done: First we have uploaded the file : HTML

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

.ts file

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

service:

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

We are getting an error: ERROR TypeError: Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.

check the screenshot: enter image description here

Please suggest some solution

Upvotes: 1

Views: 1249

Answers (1)

Padmapriya Vishnuvardhan
Padmapriya Vishnuvardhan

Reputation: 2166

readAsBinaryString(blob). In your case you didn't change the file to BLOB.

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

Upvotes: 1

Related Questions