Skip to content Skip to sidebar Skip to footer

Build Multipart/form-data Post Request In Angular2 And Validate Input Type File

I have an image (base64) that I need to send via a POST request (and wait for the response). The POST request needs to be of Content-Type:multipart/form-data. The image needs to b

Solution 1:

Form template

<form id="form" name="file" [formGroup]="FileFormGroup"(submit)="addFrom($event, FileFormGroup)" method="post">  

   <input spellcheck="true" formControlName="Demo" name="Demo"type="text"/>
   <input type="file" accept="image/*"id="file" name="File"/>
   <input formControlName="File"type="hidden"/>

</form>

Ts

import {FormGroup, FormBuilder, FormControl, Validators} from'@angular/forms';

   import {ValidatorFn} from'@angular/forms/src/directives/validators';

   publicFileFormGroup: FormGroup; /* variable */constructor(public fb: FormBuilder) {}

   ngOnInit() {
      this.FileFormGroup = this.fb.group({
      Demo: ["", Validators.required],
      File: ["", this.fileExtension({msg: 'Please upload valid Image'})]
     });
   }

   publicaddFrom(event: Event, form: FormGroup): void {

   if(form.valid && form.dirty) {

   letformTemp: HTMLFormElement <HTMLFormElement>document.querySelector('#form');

   letformData: FormData = newFormData(formTemp);

   letxhr: XMLHttpRequest = this.foo(formData);

    xhr.onreadystatechange = () => {
      if(xhr.readyState === 4) {
        if(xhr.status === 201) {
           console.log("Success");
        } else {
           console.log("Error");
        }
      }
    }
  }}

    // Foo functionpublicFoo(formData){
         leturl: Foo;
         letxhr: XMLHttpRequest = newXMLHttpRequest();
         xhr.open('POST', url, true);

         // enctype For Multipart Request
          xhr.setRequestHeader("enctype", "multipart/form-data");

          // IE bug fixes to clear cache
          xhr.setRequestHeader("Cache-Control", "no-cache");
          xhr.setRequestHeader("Cache-Control", "no-store");
          xhr.setRequestHeader("Pragma", "no-cache"); 

          xhr.send(formData);
          return xhr;
     }

     /* validation function to check proper file extension */publicfileExtension(config: any): ValidatorFn {
    return(control: FormControl) => {

      leturlRegEx: RegExp = /\.(jpe?g|png|gif)$/i;

      if(control.value && !control.value.match(urlRegEx)) {
        this.deleteImg = false;
        return {
          invalidUrl: config.msg
        };
      } else {
        returnnull;
      }
    };
  }

Solution 2:

Similar to this question here: Angular 2 - Post File to Web API

Angular2 does not yet support multipart/form-data POST requests, so I decided to use jQuery instead to implement it, and then convert it to an RxJs Observable (subject) to have the same type as what the http.post function in Angular2 should have:

//Convert Base64 Representation of jpeg to let imageData = imageString.split(',')[1];
let dataType = imageString.split('.')[0].split(';')[0].split(':')[1];
let binaryImageData = atob(imageData);
let data = newFormData();
let blob = newBlob([binaryImageData], { type: dataType })
data.append('file', blob);
let deferred = $.ajax({
  url: this._imageAPIBaseUrl,
  data: data,
  cache: false,
  contentType: false,
  processData: false,
  type: 'POST'
});
let observable = newAsyncSubject();

//When the Deferred is complete, push an item through the Observable
deferred.done(function () {

  //Get the arguments as an arraylet args = Array.prototype.slice.call(arguments);

  //Call the observable next with the same parameters
  observable.next.apply(observable, args);

  //Complete the Observable to indicate that there are no more items.
  observable.complete();
});

//If the Deferred errors, push an error through the Observable
deferred.fail(function () {

  //Get the arguments as an arraylet args = Array.prototype.slice.call(arguments);

  //Call the observable error with the args array
  observable.error.apply(observable, args);
  observable.complete();
});

return observable;

Solution 3:

Please check this working example (not mine): https://plnkr.co/edit/ViTp47ecIN9kiBw23VfL?p=preview

1 - Don't change or set the Content-Type

2 - Use FormData to send parameters

3 - Add this to app.module.ts:

import { HttpModule, RequestOptions, XHRBackend, ConnectionBackend, Http, Request, RequestOptionsArgs, Response, Headers } from'@angular/http';
@Injectable()
exportclassHttpInterceptorextendsHttp {
    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) 
    {
        super(backend, defaultOptions);
        defaultOptions.headers = newHeaders();
        defaultOptions.headers.append('Content-Type', 'application/json');
    }
}

Post a Comment for "Build Multipart/form-data Post Request In Angular2 And Validate Input Type File"