Javascript 无法通过POST api发送图像
我有一个要求,我需要从手机库/摄像头中选择一个图像,并通过POST api上传到服务器。我可以从库中加载图像,但在使用post请求发送图像时出错 我的HTML代码:Javascript 无法通过POST api发送图像,javascript,angular,ionic-framework,ionic4,angular8,Javascript,Angular,Ionic Framework,Ionic4,Angular8,我有一个要求,我需要从手机库/摄像头中选择一个图像,并通过POST api上传到服务器。我可以从库中加载图像,但在使用post请求发送图像时出错 我的HTML代码: <p (click)="selectImage()">Add an attachment</p></ion-item> 这是我通过api发送图像数据时在控制台中遇到的错误。 您应该使用FormData,并在html中使用输入类型=“文件” 您需要以FormData的形式追加数
<p (click)="selectImage()">Add an attachment</p></ion-item>
这是我通过api发送图像数据时在控制台中遇到的错误。
您应该使用FormData,并在html中使用输入类型=“文件” 您需要以FormData的形式追加数据
formData.append('file', f)
下面是输入验证的完整示例
您的服务将是这样的
public uploadFile(file: FormData): Observable {
const url = `${this.apiUrl}/upload`;
return this.http.post(url, file);
}
if (this.form.valid && files[0].name.split('.').pop() === 'excel') {
const formData = new FormData();
Array.from(files).forEach(f => formData.append('file', f));
this.uploadService.uploadPdfToGetBase64(formData).subscribe(
(res: any) => {
this.myFileInput.nativeElement.value = '';
},
(errorRes: HttpErrorResponse) => {
alert('error occured');
this.form.reset();
this.myFileInput.nativeElement.value = '';
}
);
} else {
alert('Invalid file selected. Only excel is allowed');
}
<form [formgroup]="form">
<div class="form-group">
<label for="file"></label>
<input type="file" #fileinput="" formcontrolname="fileContact" id="file" required="" (change)="handleFileInput($event.target.files)">
<button type="button" class="btn btn-info" (click)="clearSelection()">Clear</button>
</div>
</form>
您的提交函数如下所示
public uploadFile(file: FormData): Observable {
const url = `${this.apiUrl}/upload`;
return this.http.post(url, file);
}
if (this.form.valid && files[0].name.split('.').pop() === 'excel') {
const formData = new FormData();
Array.from(files).forEach(f => formData.append('file', f));
this.uploadService.uploadPdfToGetBase64(formData).subscribe(
(res: any) => {
this.myFileInput.nativeElement.value = '';
},
(errorRes: HttpErrorResponse) => {
alert('error occured');
this.form.reset();
this.myFileInput.nativeElement.value = '';
}
);
} else {
alert('Invalid file selected. Only excel is allowed');
}
<form [formgroup]="form">
<div class="form-group">
<label for="file"></label>
<input type="file" #fileinput="" formcontrolname="fileContact" id="file" required="" (change)="handleFileInput($event.target.files)">
<button type="button" class="btn btn-info" (click)="clearSelection()">Clear</button>
</div>
</form>
您的HTML如下所示
public uploadFile(file: FormData): Observable {
const url = `${this.apiUrl}/upload`;
return this.http.post(url, file);
}
if (this.form.valid && files[0].name.split('.').pop() === 'excel') {
const formData = new FormData();
Array.from(files).forEach(f => formData.append('file', f));
this.uploadService.uploadPdfToGetBase64(formData).subscribe(
(res: any) => {
this.myFileInput.nativeElement.value = '';
},
(errorRes: HttpErrorResponse) => {
alert('error occured');
this.form.reset();
this.myFileInput.nativeElement.value = '';
}
);
} else {
alert('Invalid file selected. Only excel is allowed');
}
<form [formgroup]="form">
<div class="form-group">
<label for="file"></label>
<input type="file" #fileinput="" formcontrolname="fileContact" id="file" required="" (change)="handleFileInput($event.target.files)">
<button type="button" class="btn btn-info" (click)="clearSelection()">Clear</button>
</div>
</form>
清楚的
请尝试遵循http.post…:,{data:formData},
@YunusEmreBAKAÇ尝试过,但似乎不起作用,仍然收到与您发送的JSON不同的错误。不要对你的api撒谎我已经更新了我的问题并将对象正确地传递给了我的api。那里的服务器需要文件