[英]Angular 8 upload file to Django server with FormControl
Note: I don't want to use FormData, want to use FormControl if is possible.注意:我不想使用 FormData,如果可能的话想使用 FormControl。
I have a form that send some data to Django server and it handle it and store the data.我有一个表单将一些数据发送到 Django 服务器,它处理它并存储数据。
Here is my Django funcion这是我的 Django 函数
def create(self, request):
company = CompanyForm(request.data, request.FILES)
if company.is_valid():
company = company.save()
serializer = CompanySerializer(company)
return JsonResponse(request.data, safe=False)
else:
return JsonResponse(request.errors, safe=False)
This is the Angular Form这是角形式
<form [formGroup]="companyForm" id="ngForm" enctype="multipart/form-data"
#documentEditForm="ngForm" (ngSubmit)="CreateCompany()">
//Some other fields.
<mat-form-field>
<ngx-mat-file-input formControlName="com_logo" placeholder="Logo"></ngx-mat-file-input>
<mat-icon matSuffix>folder</mat-icon>
</mat-form-field>
</form>
As you see I use the ngx-mat-file-input
for input file handling如您所见,我使用
ngx-mat-file-input
进行输入文件处理
here is the component codes for FormControl这是 FormControl 的组件代码
companyForm = new FormGroup({
com_name: new FormControl('', Validators.required),
com_owner: new FormControl('', Validators.required),
com_phone: new FormControl('', Validators.required),
com_address: new FormControl(),
com_email: new FormControl(),
com_status: new FormControl(),
com_website: new FormControl(),
com_logo: new FormControl(),
});
// From Array of errors to show on front end.
formErrors: any = {}
CreateCompany(): void {
this.apiService.createItem(this.companyForm.value, 'company').subscribe(
result => {},
error => {}
);
}
As I checked the console, I send the image info to server, but when I return the request.FILES
on Django it is empty.当我检查控制台时,我将图像信息发送到服务器,但是当我在 Django 上返回
request.FILES
时它是空的。
{com_name: "Pomtech", com_owner: "JSsss", com_phone: "072044920", com_address: "9170 N. Beacon Rd",…}
com_name: "Pomtech"
com_owner: "JSsss"
com_phone: "072044920"
com_address: "9170 N. Beacon Rd"
com_email: "n.karimi@gmail.com"
com_status: null
com_website: null
com_logo: {_files: [{}], delimiter: ", ", _fileNames: "employer-13.png"}
_files: [{}]
delimiter: ", "
_fileNames: "employer-13.png"
Also I use DjangoResetFramework, if help.如果有帮助,我也使用 DjangoResetFramework。
So what is wrong to send file data to Django and store that?那么将文件数据发送到 Django 并存储它有什么问题呢?
The backend is probably using a simple JSON parser
instead of a Multipart parser
.后端可能使用简单的
JSON parser
而不是Multipart parser
。 Check your Django settings to see what parser are you using.检查您的 Django 设置以查看您使用的是什么解析器。
from rest_framework.decorators import api_view
from rest_framework.decorators import parser_classes
from rest_framework.parsers import MultiPartParser
@api_view(['POST'])
@parser_classes([MultiPartParser])
def example_view(request, format=None):
# request.data has both files and other parsed data
return Response({'received data and files': request.data})
See Django rest framework parsers for more有关更多信息,请参阅Django 其余框架解析器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.