Django rest和Angular 5上传图像
这里获取表单的图像,我调用服务传递数据 handleFileInputfile:文件列表{ this.fileToUpload=file.item0; } CreateDetailProduct{ 让form=this.FormDetailProductCreate.value; 设detailproduct=新的detailproduct; detailproduct.Id\u Product=form.Id\u Product.Id; detailproduct.Id\u TypeProduct=form.Id\u TypeProduct.Id; detailproduct.Id\u Provider=form.Id\u Provider.Id; detailproduct.TotalPrice=form.TotalPrice; detailproduct.Quantity=表单.Quantity; detailproduct.Image=this.fileToUpload; this.servicedetailproduct.PostDetailProduct.subscribe; } 附件Django rest和Angular 5上传图像,django,angular,django-rest-framework,Django,Angular,Django Rest Framework,这里获取表单的图像,我调用服务传递数据 handleFileInputfile:文件列表{ this.fileToUpload=file.item0; } CreateDetailProduct{ 让form=this.FormDetailProductCreate.value; 设detailproduct=新的detailproduct; detailproduct.Id\u Product=form.Id\u Product.Id; detailproduct.Id\u TypeProd
在angular中的post方法中,您必须执行以下操作:
postDetailProduct(detailproduct) {
let formData: FormData = new FormData();
if (detailproduct) {
for(let key in detailproduct){
formData.append(key, detailproduct[key]);
}
}
formData.append('thumbnail', detailproduct.Image, detailproduct.Image.name);
return this._http.post(url, formData)
.pipe(
map(res => res.json())
);
}
Pd:让我知道这个解决方案是否适合你