Javascript 以angular 8格式发送json格式的数据和formData
我需要按以下格式从服务器发送数据:Javascript 以angular 8格式发送json格式的数据和formData,javascript,angular,typescript,Javascript,Angular,Typescript,我需要按以下格式从服务器发送数据: { "name":"kianoush", "userName":"kia9372", "email":"kiadr9372@gmail.com" } ------WebKitFormBoundaryhWmbFWpD25WdeB9n Content-Disposition: form-data; name="firstName" vbnvbnvb ------WebKitFormBoundaryhWmbFWpD25WdeB9
{
"name":"kianoush",
"userName":"kia9372",
"email":"kiadr9372@gmail.com"
}
------WebKitFormBoundaryhWmbFWpD25WdeB9n
Content-Disposition: form-data; name="firstName"
vbnvbnvb
------WebKitFormBoundaryhWmbFWpD25WdeB9n
Content-Disposition: form-data; name="lastName"
vbnvbnvn
------WebKitFormBoundaryhWmbFWpD25WdeB9n
Content-Disposition: form-data; name="userName"
vbnvbnvb
------WebKitFormBoundaryhWmbFWpD25WdeB9n
Content-Disposition: form-data; name="roleId"
我尝试做这项工作:
update(item:UserEditModel): Observable<any>{
const formData: FormData = new FormData();
for (const key in item) {
if (item.hasOwnProperty(key)) {
if (item[key] instanceof File) {
formData.append(key, item[key], item[key].name);
} else {
formData.append(key, item[key]);
}
}
}
return this.httpClient.post(this.appConfig.apiEndpoint+'User/UpdateUser',
formData, {
headers:this.headers,
reportProgress: true,
observe: 'events'
})
.pipe(map(response => response || {} as HttpEvent<any>));
}
但我想用以下格式从服务器发送数据:
{
"name":"kianoush",
"userName":"kia9372",
"email":"kiadr9372@gmail.com"
}
现在我如何解决这个问题???
因为您有formdata,所以它的格式不同于JSON
如果请求中没有文件,只需转换formdata即可
到JSON
或者在后端,在dto之前使用[FromForm]
通常情况下,当我有一些文件,应该去服务器
我在用这种方法你可以这样做
第一名:
slectedFile: File;
//imgUrl for showing it in the html tag
imgUrl = '../assets/img/profilepic.png';
form: FormGroup = this.fb.group({
//........
file: [null]
});
onFileSelect(file) {
if (file.target.files[0]) {
this.slectedFile = file.target.files[0] as File;
const reader = new FileReader();
reader.readAsDataURL(this.slectedFile);
reader.onload = (event: any) => {
this.imgUrl = event.target.result;
};
}
}
秒:
slectedFile: File;
//imgUrl for showing it in the html tag
imgUrl = '../assets/img/profilepic.png';
form: FormGroup = this.fb.group({
//........
file: [null]
});
onFileSelect(file) {
if (file.target.files[0]) {
this.slectedFile = file.target.files[0] as File;
const reader = new FileReader();
reader.readAsDataURL(this.slectedFile);
reader.onload = (event: any) => {
this.imgUrl = event.target.result;
};
}
}
第三名:
slectedFile: File;
//imgUrl for showing it in the html tag
imgUrl = '../assets/img/profilepic.png';
form: FormGroup = this.fb.group({
//........
file: [null]
});
onFileSelect(file) {
if (file.target.files[0]) {
this.slectedFile = file.target.files[0] as File;
const reader = new FileReader();
reader.readAsDataURL(this.slectedFile);
reader.onload = (event: any) => {
this.imgUrl = event.target.result;
};
}
}
最后一个:
slectedFile: File;
//imgUrl for showing it in the html tag
imgUrl = '../assets/img/profilepic.png';
form: FormGroup = this.fb.group({
//........
file: [null]
});
onFileSelect(file) {
if (file.target.files[0]) {
this.slectedFile = file.target.files[0] as File;
const reader = new FileReader();
reader.readAsDataURL(this.slectedFile);
reader.onload = (event: any) => {
this.imgUrl = event.target.result;
};
}
}
我想你的主要问题在这里
我应该提到的是,我个人使用这个软件包进行输入
在html方面,您只需说:
<mat-form-field class="col-md-12 ml-10 ngxmatfileinput">
<ngx-mat-file-input
(change)="onFileSelect($event)"
formControlName="file"
accept="image/*">
</ngx-mat-file-input>
<img [src]="imgUrl" class="float-left icon-fileupload" />
</mat-form-field>
如果不发送文件,请使用以下代码
update(item:UserEditModel): Observable<any>{
const _data ={
"name":"item.name",
"userName":"item.userName",
"email":"item.email"
}
return this.httpClient.post(this.appConfig.apiEndpoint+'User/UpdateUser',
_data)
.pipe(map(response => response || {} as HttpEvent<any>));
}
更新(项目:UserEditModel):可观察{
常数数据={
“名称”:“项目名称”,
“用户名”:“item.userName”,
“电子邮件”:“item.email”
}
返回this.httpClient.post(this.appConfig.apidendpoint+'User/UpdateUser',
_(数据)
.pipe(map(response=>response | |{}作为HttpEvent));
}