* В угловом выражении * 1000 вы должны сделать следующее:
шаблон:
Эти строки являются частью тега формы.
<div class="mb-3">
<label>Logo (optional):</label>
<input type="file"
id="logo"
name="logo"
(change)="handleFileInput($event.target.files)">
</div>
ts:
handleFileInput(files: FileList) {
let fileItem = files.item(0);
let fileNameArr = fileItem.name.split('.');
if(fileNameArr[1] == 'jpg' || fileNameArr[1] == 'JPG' || fileNameArr[1] == 'png' || fileNameArr[1] == 'PNG'){
this.errorMessage = '';
this.fileToUpload = fileItem;
}else{
this.errorMessage = 'logo should be in jpg or png format';
this.fileToUpload = null;
}
функция, которая вызывает API-сервис:
signUpForm(form: NgForm){
let newUser = {
full_name: form.value.full_name,
facility_name: form.value.facility_name,
};
let formData: FormData = new FormData();
// logo file is optional
if(this.fileToUpload)
formData.append('logo_company', this.fileToUpload, this.fileToUpload.name);
for(let key in newUser){
formData.append(key, newUser[key]);
}
//call membership service for sign up
this.membershipService.registerUser(formData).subscribe(
(data: any) => {
// this.spinnerService.hide();
this.isDisplaySpinner = false;
window.alert('You signed up to web site successfully.\nAn activation link sent to your email. Refer to your email please and click on it.');
this.router.navigate(['/']);
},
(error: any) => {
// this.spinnerService.hide();
this.isDisplaySpinner = false;
if(error.email)
this.errorMessage = error.email;
else
this.errorMessage = error;
}
);
}
В классе сериализатора не определяют cat_image,
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('id', 'cat_name', 'description', 'parent_cat_id', 'cat_image', 'active')