gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:45:42 26 4
gpt4 key购买 nike

我有一张图片 (base64),需要通过 POST 请求发送(并等待响应)。 POST 请求需要是 Content-Type:multipart/form-data。图片需要是Content-Type: image/jpg

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

以二进制图像数据为内容主体。

我正在尝试使用 Angular 2 的 Http Post 方法,但我不太确定如何生成请求。这是我的:

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

我可以说我遗漏了其中的一部分,但我不确定我需要做什么才能为二进制图像数据提供 Content-Disposition & Type 等。

最佳答案

表单模板

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

关于javascript - 在 Angular2 中构建 multipart/form-data POST 请求并验证输入类型文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36900989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com