简体   繁体   English

在Angular2中构建multipart / form-data POST请求并验证输入类型File

[英]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). 我有一个图像(base64),我需要通过POST请求发送(并等待响应)。 The POST request needs to be of Content-Type:multipart/form-data . POST请求必须是Content-Type:multipart/form-data The image needs to be of Content-Type: image/jpg 图像需要是Content-Type: image/jpg

The POST request should look like: POST请求应如下所示:

POST https://www.url... HTTP/1.1
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: www.host.com
Content-Length: 199640

---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="Nikon Digital SLR Camera D3100 14.2MP 2.jpg"
Content-Type: image/jpeg

With the binary image data as the content body. 用二进制图像数据作为内容体。

I'm attempting to use the Http Post method of angular 2, but I'm not quite sure about how to generate the request. 我正在尝试使用角度2的Http Post方法,但我不太确定如何生成请求。 This is what I have: 这就是我所拥有的:

let body = atob(imageData);
let headers = new Headers({'Content-Type': 'multipart/form-data'});
let options = new RequestOptions({headers: headers});

this._http.post(url, body, options)
.map(res=>{
  //do stuff
});

I can tell that I am missing part of it but I am not sure what I need to do to give the binary image data it's Content-Disposition & Type etc. 我可以说我错过了它的一部分,但我不知道我需要做什么来给二进制图像数据它的内容 - 处置和类型等。

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

   public FileFormGroup: FormGroup; /* variable */

   constructor(public fb: FormBuilder) {}

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

   public addFrom(event: Event, form: FormGroup): void {

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

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

   let formData: FormData = new FormData(formTemp);

   let xhr: XMLHttpRequest = this.foo(formData);

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

    // Foo function
     public Foo(formData){
         let url: Foo;
         let xhr: XMLHttpRequest = new XMLHttpRequest();
         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 */

  public fileExtension(config: any): ValidatorFn {
    return (control: FormControl) => {

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

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

Similar to this question here: Angular 2 - Post File to Web API 与此问题类似: Angular 2 - 将文件发布到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: Angular2还不支持multipart / form-data POST请求,所以我决定使用jQuery来实现它,然后将它转换为RxJs Observable(subject),使其与Angular2中的http.post函数的类型相同有:

//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 = new FormData();
let blob = new Blob([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 = new AsyncSubject();

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

  //Get the arguments as an array
  let 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 array
  let args = Array.prototype.slice.call(arguments);

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

return observable;

Please check this working example (not mine): https://plnkr.co/edit/ViTp47ecIN9kiBw23VfL?p=preview 请查看此工作示例(不是我的): https//plnkr.co/edit/ViTp47ecIN9kiBw23VfL?p = preview

1 - Don't change or set the Content-Type 1 - 不要更改或设置Content-Type

2 - Use FormData to send parameters 2 - 使用FormData发送参数

3 - Add this to app.module.ts: 3 - 将其添加到app.module.ts:

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

暂无
暂无

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

相关问题 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios 文件上传ajax:您的POST请求的正文格式不正确的multipart / form-data - file upload ajax:The body of your POST request is not well-formed multipart/form-data 我应该如何解码文件的内容以将其包含在 multipart/form-data POST 请求中? - How should I decode the contents of a file to include it in a multipart/form-data POST request? 如何设置POST的MIME类型-axios中的multipart / form-data? - How to set MIME type for POST - multipart/form-data in axios? 客户端设置了multipart / form-data请求的文件部分的内容类型错误 - Content type for file part of the multipart/form-data request is set wrong by the client 当我输入enctype =“multipart / form-data”和输入[type =“file”]时,使用PassportJS“丢失凭据” - “Missing credentials” with PassportJS when I put enctype=“multipart/form-data” and an input[type=“file”] POST文件多部分/表单数据(JavaScript)时出错 - Error when POST file multipart/form-data (JavaScript) 强大:解析多部分/表单数据请求,然后上传文件 - Formidable: Parse multipart/form-data request THEN upload file Request.js模拟文件上传(多部分/表单数据) - Request.js Simulate a File Upload (multipart/form-data) 将收到的文件发送到 axios multipart/form-data 请求 - Send received file to an axios multipart/form-data request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM